Skip to content

Instantly share code, notes, and snippets.

@gogomako
Created May 1, 2014 03:49
Show Gist options
  • Save gogomako/e4878ba38a19f995113e to your computer and use it in GitHub Desktop.
Save gogomako/e4878ba38a19f995113e to your computer and use it in GitHub Desktop.
/**
* @brief This function is the main function of this script set. For those who cannot read script, I think this name is better than main.
**/
function choose_Me_to_Backup(){
readTable();
}
/**
* @brief This function can read a sheet table in the file
*
* @return void
*
* @warning This is a private function. Do not set this function as a entry function.
**/
function readTable(){
var sheet = SpreadsheetApp.getActiveSheet();
var maxRows = sheet.getMaxRows();//the max means the max range you can edit in the sheet
var maxCols = sheet.getMaxColumns();
var tableName = sheet.getSheetName();
var tableId = sheet.getSheetId();
var numColumns = sheet.getLastColumn()
var numRows = sheet.getLastRow();
var tableValue = "";
for (var i = 1; i <= numRows ; i++) {// the index of rows and columns are start from 1
tableValue = tableValue + sheet.getSheetValues(i, 1, 1, 1).toString();
for (var j = 2; j <= numColumns ; j++){
tableValue = tableValue + "," + sheet.getSheetValues(i, j, 1, 1).toString();
}
tableValue = tableValue + "\n";
}
var address = "[email protected]";
var title = "backup test";
var msg = tableValue;
mailTo(address,title,msg);
}
/**
* @brief This function can create a mail and mail it
*
* @para (String)address is the mail address to mail to
* @para (String)title is the subject of mail
* @para (String)msg is the body of mail
*
* @return void
* @warning This is a private function. Do not set this function as a entry function.
**/
function mailTo(address,title,msg){
var d = new Date();
var currentDate = d.toLocaleDateString();
var currentTime = d.toLocaleTimeString();
GmailApp.sendEmail(address , title ,"Backup Time Stamp:\t" + currentDate + "\t" + currentTime + "\n" +
"\n" +
"---------------------------------------------------------------------------\n" +
"\n" +
msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment