Vous êtes sur la page 1sur 4

function sendEmail() {

//
===================================================================================
=
// This script is customized to work with Tiller's Budget Dashboard Sheet Version
1.0.0
// No changes are required below.
//
// The script can be customized for other sheets by adjusting the settings below.
// Fill in this top section with your custom information
//
===================================================================================
=

// Only change the values INSIDE the " " (double-quotes)


// And make sure the lines end with a ;

// By default, the script will send an email to the owner of the sheet.
// But you can sent a custom email or multiple custom emails
//
// Default setting
// var customEmail = "none";
//
// Single email example
// var customEmail = "test@test.com";
//
// Multiple email example - each email address must be separated by commas
// var customEmail = "first@test.com,second@test.com";

var customEmail = "none";

// By default, the script uses the sheet named Budget


// But you can enter the name of any sheet you want to email
// Make sure to match the name exactly and with the same upper and lower case
letters
// Default setting
// var sheetName = "Budget";

var sheetName = "Budget";

// By default, the script uses the email subject "Tiller Budget Dashboard Email"
// But you can use any subject line you want
// Default setting
// var subject = "Tiller Budget Dashboard Email";

var subject = "Tiller Budget Dashboard Email";

// You can set the alignment method for the cells in the email
// The default Budget sheet uses the custom alignment
//
// var alignment = "left"; puts all the columns aligned left
// var alignment = "right"; puts all the columns aligned right
// var alignment = "custom"; let's you set the set the alignment for each column

var alignment = "custom";

// If you set alignment to "custom", set a value [left or right or skip] for each
column in your sheet separated by a comma.
// The skip settings lets you skip a column so it won't display
// Make sure each column has a value or else it won't work
// This is the default setting for the Budget sheet
// var customAlign =
["skip","left","skip","right","right","right","skip","skip","right"];

var customAlign =
["skip","left","skip","right","right","right","skip","skip","right"];

// The email can display call rows and columns OR you can set the number of rows
and columns manually
// To display all rows and column, use
// var rowDisplay = "all";
// var columnDisplay = "all"
//
// To manually set the number of rows or columns, enter the number you want
// Don't use the letter for the column. Count the number of columns instead.
// If entering a number, you don't need quotes around it.
// The default settings for the Budget email are:
// var rowDisplay = "all";
// var columnDisplay = 9;

var rowDisplay = "all";


var columnDisplay = 9;

// This setting is used for the default Budget template email


// By default it is set to true
// If you are using this for a different sheet, set it to false
// Quotes are not needed around the word true or false
// Default
// var customBudgetSheet = true;

var customBudgetSheet = true;

// ================================================================
// This ends the configuration section
// DO NOT make any changes below unless you know what you are doing
// ================================================================

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);

var range = sheet.getDataRange();


var values = range.getDisplayValues();

// this gets an array of font weights, normal or bold, for each cell
var weights = range.getFontWeights();

// this is used for the budget template to determine if a row is blank


// it checks column B for a category name
var blankRowCount = 0;

if (rowDisplay == 'all')
{ rowDisplay = range.getLastRow(); }

if (columnDisplay == 'all')
{ columnDisplay = range.getLastColumn(); }
var message = '';

// only display the custom message for the default budget tempalte
if (customBudgetSheet)
{
// this custom message is for the budget template
var customBudgetMessage = "<b>" + values[6][11] + "</b> needs Categorizing<br
/>\n" +
"<b>" + values[8][10] + "</b> are waiting for me to categorize.<br
/>\n" +
"Net Cash Flow: <b>" + values[14][11] + "</b><br />\n" +
values[16][10] + " " + values[17][10] +"<br />\n" +
values[18][10] + "<br />\n" +
"Expense Budget Remaining: <b>" + values[23][11] + "</b><br />\n" +
values[25][10] + "<br />\n" +
values[26][10] + "<br />\n";

message = message + customBudgetMessage;


}

// this starts the table for the cells


message = message + "<table rules='all' style='border-color: #666;'
cellpadding='5'>\n";

for (var i = 0; i < rowDisplay; i++ )


{
// Test if: the category in column B , values[i][1] is blank AND
// there was a blank row in the prior row
// and this is greater than row 7 because earlier rows have 2 empty
lines
// and this is the default Budget sheet
// If all that is true, don't display any row
// This solves the problem of empty blank rows on the bottom of the email

if (values[i][1] == '' && blankRowCount == 1 && i > 8 && customBudgetSheet)


{
// don't display row
}
else
{ // display the row
message = message + "<tr>\n";

for (var j = 0; j < columnDisplay; j++ )


{

if (customAlign[j] != 'skip')
{
message = message + "<td style='text-align: ";

if (alignment == 'custom')
{
message = message + customAlign[j];
}
else
{
message = message + alignment;
}
message = message + "; font-weight: " + weights[i][j] + ";'>" + values[i]
[j] + "</td>\n";
} // ends if no skip
} // ends the for loop for each column

message = message + "</tr>\n";

// test of no category for the budget template


if (customBudgetSheet)
{
if (values[i][1] == '')
{blankRowCount = 1;}
else
{blankRowCount = 0;}
}
}
}
message = message + "</table>\n";
// use the users email or the custom Email
if (customEmail == 'none')
{var email = Session.getActiveUser().getEmail();}
else
{var email = customEmail;}

// send the email


MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: message});
}

Vous aimerez peut-être aussi