Created
May 19, 2016 14:05
-
-
Save joshjenkinsAR/a1992f9106e649f6bfad928968cdddc3 to your computer and use it in GitHub Desktop.
A Google App Script to copy a sheet to another folder, email if values are present, and clear the original file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function copyemailclear(){ | |
var file = DriveApp.getFileById('Orignal File ID'); | |
var destFolder = DriveApp.getFolderById("Destination File ID"); | |
var todaylong = new Date(); | |
var today = todaylong.toISOString(); | |
var name= "new file name "+today; // Add a date string to the end of the file name | |
var new_url = DriveApp.getFileById(id).makeCopy(name, destFolder).getUrl(); | |
// Subject of email message | |
var subject = "Email Subject " + (new Date()).toString(); | |
// Get the currently active spreadsheet URL (link) | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var html = 'Email content'; | |
// Email Body can be HTML | |
var body = html; | |
var url = ss.getUrl(); | |
url = url.replace(/edit$/,''); | |
var url_ext = 'export?exportFormat=csv&format=csv'; // Prepare file attachment for email | |
var token = ScriptApp.getOAuthToken(); | |
// Convert the main worksheet to CSV and the whole document as an XLSX | |
var response = UrlFetchApp.fetch(url + url_ext, { | |
headers: { | |
'Authorization': 'Bearer ' + token | |
} | |
}); | |
//convert the responses to a blob and store in our array | |
var csvfile = response.getBlob().setName('Summary Export' + '.csv'); | |
var xlsx_ext = 'export?exportFormat=xlsx&format=xlsx'; | |
var xlsx_response = UrlFetchApp.fetch(url + xlsx_ext, { | |
headers: { | |
'Authorization': 'Bearer' + token | |
} | |
}); | |
var xlsxfile = xlsx_response.getBlob().setName('Detailed Export' + '.xlsx'); // Also include a multiple page export as an xlsx | |
// Define the scope | |
Logger.log("Storage Space used: " + DriveApp.getStorageUsed()); | |
//Check to see if there were actually any new data | |
var totalssheet = SpreadsheetApp.getActiveSheet(); | |
// figure out what the last row is | |
var lastRow = totalssheet.getLastRow(); | |
// the rows are indexed starting at 1, and the first row | |
// is the headers, so start with row 2 | |
var startRow = 2; | |
// grab column 3 | |
var range = totalssheet.getRange(2,3,lastRow-startRow+1,1 ); | |
var numRows = range.getNumRows(); | |
var totals = range.getValues(); | |
var send_email = 0; | |
// Loop over the totals values | |
for (var i = 0; i <= numRows - 1; i++) { | |
var total = totals[i][0]; | |
if(total > 0) { | |
send_email++; | |
} | |
} | |
if( send_email > 0 ) { | |
GmailApp.sendEmail('Comma separate email addresses', subject, body, {attachments: [file.getAs(MimeType.PDF), csvfile]} ); | |
} | |
//////////////Clear all selected ranges on all sheets, except the first one | |
var ss = SpreadsheetApp.getActive(); | |
var allsheets = ss.getSheets(); | |
for (var s in allsheets){ | |
for(var i=1; i < allsheets.length ; i++) { | |
var sheet=allsheets[i] | |
sheet.getRange('A2:Z99').clear({ | |
contentsOnly: true | |
}) | |
} | |
}//end clearing loop | |
}// end of function... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment