Create a PDF of the unhidden sheets Google Script - google-apps-script

I have created a simple emailing script for a google sheet. I need it to attach the PDF of the active file, but only 3 sheets (the 3 unhidden sheets). I have tried several things but nothing seems to work, I am honestly not an expert in Google script so I may be missing something obvious. Following is my code:
function sendEmails() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Email');
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = row[2];
var file= SpreadsheetApp.getActive();
MailApp.sendEmail(emailAddress, subject, message,{cc:"example#gmail.com",attachments:[file.getAs(MimeType.PDF)]});
}
}
What it does is attach the whole spreadsheet as a PDF, which doesn't help me because there are a lot of hidden sheets that I don't need in the PDF. Please help me!! Thanks!

The function will send emails to recipient in the Emails Sheet which also contains the subject and the message. It also creates a PDF of the current Spreadsheet with the sheets that are in the incl array. After the email is sent it returns the sheets back to their original visibility state.
function emailAsPDF() {
var ss=SpreadsheetApp.getActive();
var incl=['Sheet2','Emails'];
var pdfFolderId="Folder Id";
var folder=DriveApp.getFolderById(pdfFolderId);
var sObj={shA:[]};
var shts=ss.getSheets();
shts.forEach(function(sh,i){
sObj.shA.push(sh.getName());
if(sh.isSheetHidden()) {
sObj[sh.getName()]='hide';
}else{
sObj[sh.getName()]='show';
}
if(incl.indexOf(sh.getName())!=-1) {
sh.showSheet();
}else{
sh.hideSheet();
}
});
var file=folder.createFile(ss.getBlob()).setName(ss.getName()).getAs(MimeType.PDF);
var sh=ss.getSheetByName('Emails');
var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
var hA=sh.getRange(1,1,1,sh.getLastColumn()).getValues()[0];
var hObj={};
hA.forEach(function(e,i){if(e){hObj[e]=i;}})
var vA=rg.getValues();
vA.forEach(function(r,i){
var recipient=r[hObj.recipient];
var subject=r[hObj.subject];
var message=r[hObj.message];
GmailApp.sendEmail(recipient, subject, message, {attachments:[file]})
})
sObj.shA.forEach(function(name,i){
if(sObj[name]=='hide') {
ss.getSheetByName(name).hideSheet();
}else{
ss.getSheetByName(name).showSheet();
}
});
}
This is an image of my Emails sheet:
I know...It's pretty trivial but you can improve it as you wish.

Related

google script specify active sheet

and thanks for the help. I've got a noob question, and need a noob answer.
Trying to email a specific google sheet as a pdf weekly, but script emails out whatever sheet happens to be open at the time.
Stole various snippets of code, here's what I've got: (And no, I don't think that this block of code was formatted and posted correctly.)
function endOfWK_1 () {
//This script converts all formulas to values in the currently displayed sheet, then converts the currently displayed sheet to a pdf, then emails the
pdf as an attachment to the addresses shown in cell B17 in the "Email" sheet.
//Replace all formulas in range "WK 1!A6:A29" with values
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('WK 1');
var range = sheet.getRange("WK 1!A6:A29");
range.copyTo(range, {contentsOnly: true});
// FOR WK1 ONLY!!!
// Set the Active Spreadsheet so we don't forget
var originalSpreadsheet = SpreadsheetApp.getActive();
// Set the message to attach to the email.
var message = "Please see attached.";
// Get Dates from Email!B5
var period = originalSpreadsheet.getRange("Email!B5").getValues();
// Construct the Subject Line
var subject = period;
// Get contact details from "Email" sheet and construct To: Header
var contacts = originalSpreadsheet.getSheetByName("Email");
var numRows = contacts.getLastRow();
var emailTo = contacts.getRange(17, 2, numRows, 1).getValues();
// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet);
// Find and delete the default "Sheet1"
newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();
// Create the PDF, currently called "Tracking Sheet.pdf"
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Tracking Sheet.pdf',content:pdf, mimeType:'application/pdf'};
// Send the freshly constructed email
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
// Delete the sheet that was created
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
// Write the date and time that the script ran
var date = sheet.getRange('Statistics!A1').getValues();
SpreadsheetApp.getActiveSheet().getRange('Analysis!E5').setValues(date);
}
This is a bound script, attached to a google workbook containing 5 sheets. My problem is that my script always emails the sheet that happens to be open at the time.
I want to email one specific sheet, whether the workbook is open or closed. How can I do this? (I hope to install a trigger to make this script run automatically.)
Also, anyone want to critique my code?
Thanks to all.
I've fixed it up a little and added some comments. There were a lot of little things I fixed up biggest thing was that you should reuse variables that you've created.
This hasn't been tested...
function endOfWK_1 () {
//Replace all formulas in range "WK 1!A6:A29" with values
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetWK1 = activeSpreadsheet.getSheetByName('WK 1');
var range = sheetWK1.getRange("WK 1!A6:A29");
range.copyTo(range, {contentsOnly: true});
// FOR WK1 ONLY!!!
// Set the Active Spreadsheet so we don't forget
var originalSpreadsheet = SpreadsheetApp.getActive(); //this should probably be changed depending on what sheet you are trying to access: activeSpreadsheet.getSheetByName('Email')
// Set the message to attach to the email.
var message = "Please see attached.";
// Get Dates from Email!B5
var period = originalSpreadsheet.getRange("Email!B5").getValues();
// Construct the Subject Line
var subject = period;
// Get contact details from "Email" sheet and construct To: Header
var contacts = originalSpreadsheet.getSheetByName("Email");
var numRows = contacts.getLastRow();
var emailTo = contacts.getRange(17, 2, numRows, 1).getValues();
// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export"); //Create a spreadsheet to copy to
// var originalSheet = activeSpreadsheet.getSheetByName("WK1"); Already defined above as sheetWK1
//var projectname = SpreadsheetApp.getActiveSpreadsheet(); Seems like this is not used.
sheetWK1.copyTo(newSpreadsheet); //Take the original sheet and copy it to the newSpreadsheet
// Find and delete the default "Sheet1"
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Sheet1")); //We can just call the deleteSheet method.
// Create the PDF, currently called "Tracking Sheet.pdf"
var pdf = newSpreadsheet.getAs('application/pdf').getBytes(); //No need to get the Spreadsheet object again, as we alreat have it!
var attach = {fileName: 'Tracking Sheet.pdf', content: pdf, mimeType: 'application/pdf'};
// Send the freshly constructed email
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
// Delete the sheet that was created
newSpreadsheet.setTrashed(true); //Again no need to find the object. We have it.
// Write the date and time that the script ran
var date = sheet.getRange('Statistics!A1').getValues();
activeSpreadsheet.getRange('Analysis!E5').setValues(date);
}
The main issue was var originalSpreadsheet = SpreadsheetApp.getActive(); you are getting the active sheet and using that to create you pdf.
EDIT: I've cleaned up the whole thing a little and ended up with this. It hasn't been tested.
function endOfWK_1 () {
//Replace all formulas in range "WK 1!A6:A29" with values
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetWK1 = activeSpreadsheet.getSheetByName('WK 1');
var emailSheet = activeSpreadsheet.getSheetByName("Email");
var range = sheetWK1.getRange("WK 1!A6:A29");
range.copyTo(range, {contentsOnly: true});
// FOR WK1 ONLY!!!
// Set the message to attach to the email.
var message = "Please see attached.";
// Get Dates from Email!B5
var period = emailSheet.getRange("Email!B5").getValues();
// Construct the Subject Line
var subject = period;
// Get contact details from "Email" sheet and construct To: Header
var numRows = emailSheet.getLastRow();
var emailTo = emailSheet.getRange(17, 2, numRows, 1).getValues();
// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
sheetWK1.copyTo(newSpreadsheet);
// Find and delete the default "Sheet1"
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Sheet1"));
// Create the PDF, currently called "Tracking Sheet.pdf"
var pdf = newSpreadsheet.getAs('application/pdf').getBytes();
var attach = {fileName: 'Tracking Sheet.pdf', content: pdf, mimeType: 'application/pdf'};
// Send the freshly constructed email
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
// Delete the sheet that was created
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
// Write the date and time that the script ran
var date = activeSpreadsheet.getRange('Statistics!A1').getValues();
activeSpreadsheet.getRange('Analysis!E5').setValues(date);
}

google sheets script conditionally move row to another spreadsheet

I am new to this and I am having some trouble trying to figure this out. I have a spreadsheet that collects data from a google form. I am trying to find a way to move that data based on a column answer to a different google sheet. (Not a sheet in the same document but a different document all together). It seems like there is some information about moving to a different tab in the same document, but not a different document.
I will first say that I tried just an IMPORTRANGE function, but that only mirrors the data, and does not let you update or change cells.
Here is what I have been able to piece together so far, but I may be way off.
I have a trigger that would run every hour.
function myFunction() {
var ss = SpreadsheetApp.openById('1U0I9SkbGkHgm-vRkwf2Ppc_yxlqrVlg2t8yKRy3sYuI');
var sheetOrg = ss.getSheetByName("Form Responses 1");
var value1ToWatch = "ANDERSON";
var value2ToWatch = "BARNES";
var sheetNameToMoveTheRowTo = "Sheet1"; //sheet has same name for each target openByID(" *url key* ")
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ts1 =SpreadsheetApp.openById("1PxV1PQrMdu_2aSru4dpan8cUgHYdlYPUp5anyzjMAms");
sheet1in = ts1.getSheetByName("Sheet1");
var ts2 = SpreadsheetApp.openById("1BYyQZNiXc2QqsyqWs7uazjl5B6mfFYM1xj3u8gWyYOQ");
sheet1in = ts2.getSheetByName("Sheet1");
arr = [],
values = sheetOrg.getDataRange().getValues(),
i = values.length;
while (--i) {
if (value1ToWatch.indexOf(values[i][1]) > -1) {
arr.unshift(values[i])
sheetOrg.deleteRow(i + 1)
sheet1in.getRange(sheet1in.getLastRow()+1, 1, arr.length,
arr[0].length).setValues(arr);
};
if (value2ToWatch.indexOf(values[i][1]) > -1) {
arr.unshift(values[i])
sheetOrg.deleteRow(i + 1)
sheet2in.getRange(sheet2in.getLastRow()+1, 1, arr.length,
arr[0].length).setValues(arr);
};
}
}
The sheet google forms dumps the information into is "Form Responses 1".
Column B is the cells I want to get the values from. (There are a total of 9 different values that it can be like "ANDERSON", "BARNES", "SMITH", etc).
For sheetNameToMoveTheRowTo is "Sheet1" - that may be confusing and I may need to change that, but for example
ts1 =SpreadsheetApp.openById("1PxV1PQrMdu_2aSru4dpan8cUgHYdlYPUp5anyzjMAms") the sheet name that I want the information moved to is "Sheet1".
ts2 = SpreadsheetApp.openById("1BYyQZNiXc2QqsyqWs7uazjl5B6mfFYM1xj3u8gWyYOQ") the sheet name is also "Sheet1" but in a different document.
I think if I were able to get the "ANDERSON" one to work, then I can just add additional variables for each possible response, and just copy and paste additional "IF" statements, just changing the valueToWatch and targetSheet values. <= if that is not correct please let me know
I have tried to both debug, and run the script above but nothing happens. There are no errors reported on the debug, but it is not moving any information over.
Any idea what I am doing wrong?
// UPDATE I got this to work. I have updated the code listed with what worked for me.
I think that copyTo() method will not work like you mentioned, it operates on same SpreadSheet. I'm sending you example with looping on source sheet data and then setting the target sheet values with it.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var allrange = ss.getActiveRange();
var range = sheet.getRange("A1:A10");
var values = range.getValues();
var allvals = [];
for(var i = 0; i < values.length; i++) {
allvals.push( values[i] ) ;
}
var dataLength = allvals.length;
// alert data
var ui = SpreadsheetApp.getUi();
// ui.alert( JSON.stringify(allvals) )
// copy to new Google SpreadSheet
var newSheet = SpreadsheetApp.openById('1F79XkNPWm2cCWlB2JP3K4tAYRESKUgtHK4Pn2vbJEiI').getSheets()[0];
var tmp = "A1:A" + dataLength ;
var newRange = newSheet.getRange( tmp );
newRange.setValues( allvals );
}
Here's a simple example of moving data from one spreadsheet to another.
function movingDataToSS(){
var ss=SpreadsheetApp.getActive();
var dss=SpreadsheetApp.openById('ssId');
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
var dsh=dss.getSheetByName('Sheet1');
var drg=dsh.getRange(1,1,rg.getHeight(),rg.getWidth()).setValues(vA);
}
If you interested in placing some conditions on your output by only getting output from odd rows and even columns.
function movingDataOddRowsAndEvenCols(){
var ss=SpreadsheetApp.getActive();
var dss=SpreadsheetApp.openById('ssId');
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
var h=rg.getHeight();
var w=rg.getWidth();
var dsh=dss.getSheetByName('Sheet1');
for(var i=0;i<h;i++){
var out=[];
if(i%2==0){
for(var j=0;j<w;j++){
if(j%2==1){
out.push(vA[i][j]);
}
}
dsh.appendRow(out);
}
}
}

Create New Sheet with Name Based on Form Submission

In Google Sheets I have a script I'm using to create a new sheet for each Google Form that is submitted. It is supposed to create a new sheet with name based on the last column, column G(which isn't a form submitted column). Then it takes the information from the last row and the heading row and copy it to the first two rows of the created sheet. It also adds formulae to cells to put the info into columns(transpose) and format it based on a created sheet.
Right now it is creating the sheet and copying the formulae and format, but not giving it the proper name or pulling the last row information.
Please help!
~Charles
I have copied the code below:
function onFormSubmit() {
// onFormSubmit
// get submitted data
var ss = SpreadsheetApp.openById(
'...');
var sheet = ss.getSheetByName("Responses");
var headings = sheet.getRange(1,1,1,
sheet.getLastColumn()).getValues();
var lastRow = sheet.getRange(sheet.getLastRow(),1,1,
sheet.getLastColumn()).getValues();
var studentUsername = lastRow[0][6];
// check if username has sheet
if(ss.getSheetByName(studentUsername)){
var userSheet = ss.getSheetByName(studentUsername);
// if not make
} else {
var userSheet = ss.insertSheet(studentUsername);
userSheet.getRange(1,1,1,
headings[0].length).setValues(headings);
}
// copy submitted data to user's sheet
userSheet.appendRow([lastRow]);
userSheet.appendRow(['=CONCATENATE(B6," ",B5)']);
userSheet.appendRow(['=TRANSPOSE(B1:2)']);
userSheet.hideRows(1,2);
userSheet.setColumnWidth(1, 500);
userSheet.setColumnWidth(2, 500);
var FormatSheet = ss.getSheetByName("Format");
var FormatRange = FormatSheet.getRange("a3:b28");
FormatRange.copyFormatToRange(userSheet,1,3,3,28);
}
With some help from the comments and playing around, i figured out the code I need. Big thanks to #Cooper!
Here it is:
function onFormSubmit() {
// onFormSubmit
// get submitted data
var ss = SpreadsheetApp.openById(
'Sheet_ID');
var sheet = ss.getSheetByName("Responses");
var row = sheet.getLastRow();
var Col = sheet.getLastColumn();
var headings = sheet.getRange(1,1,1,
Col).getValues();
var lastRow = sheet.getRange(row, 1, 1, Col);
var studentUsername = sheet.getRange(row, Col).getValue();
// check if username has sheet
if(ss.getSheetByName(studentUsername)){
var userSheet = ss.getSheetByName(studentUsername);
// if not make
} else {
var userSheet = ss.insertSheet(studentUsername);
userSheet.getRange(1,1,1,
headings[0].length).setValues(headings);
}
// copy submitted data to user's sheet
userSheet.appendRow(lastRow.getValues()[0]);
userSheet.appendRow(['=CONCATENATE(B6," ",B5)']);
userSheet.appendRow(['=TRANSPOSE(B1:2)']);
userSheet.hideRows(1,2);
userSheet.setColumnWidth(1, 500);
userSheet.setColumnWidth(2, 500);
var FormatSheet = ss.getSheetByName("Format");
var FormatRange = FormatSheet.getRange("a3:b28");
FormatRange.copyFormatToRange(userSheet,1,3,3,28);
}

Send content of Google Sheet with Google App Script

I'm trying to create a script that'll send me the content of a google sheet by email on a weekly basis.
Can someone point me in the right direction?
EDIT: It needs to be in the in the Body
This will send the sheet as a xls to your email. Get this to work then set a time based trigger. - also notice this is a private sheet, that gets set public for a brief moment while it emails and then sets back to private.
function getGoogleSpreadsheetAsExcel(){
try {
var ss = SpreadsheetApp.getActive();
var sheet = DriveApp.getFileById(ss.getId());
// sets sharing to public - to send out email.
sheet.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
var url = "https://www.googleapis.com/drive/v3/files/" + ss.getId() + "/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&key=" + "YOUR_KEY_GOES_HERE";
var blob = UrlFetchApp.fetch(url).getBlob();
Logger.log(url);
blob.setName(ss.getName() + ".xlsx");
var now = new Date();
MailApp.sendEmail("YOUR_EMAIL_ADDRESS_GOES_HERE", "Backup " + now , "Backup " + now , {attachments: [blob]});
} catch (f) {
Logger.log(f.toString());
}
// returns the file back to Private access
sheet.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT);
}
Here's my solution so far but it's not clean & far from ideal:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
var data = range.getValues();
var body = '';
for( var row in data ) {
for( var col in data[row] ) {
body += data[row][col] + '\t';
}
}
body += '\n';
}
Logger.log(body);
// MailApp.sendEmail('your#email.com', 'Director Dealings', body);
}
You may check this tutorial on how to send emails from a Spreadsheet.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
You need to use the MailApp.sendEmail(). Just be noted that there are four versions of this method

sendEmail function in Google Sheets

I have been struggling for days to get this appScript to function within my Google Sheets. Currently it is getting "stuck" on MailApp.sendEmail line, particularly on emailAddress. It gives me this error message "Invalid email: undefined". I based the majority of the script on their Sending Emails from a Spreadsheet Tutorial. So I'm not too sure where it might be going wrong.
Any help is much appreciated!
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("Candidates Ready for Offer");
var startRow = 2; // First row of data to process
var numRows = ss.getSheetByName("Candidates Ready for Offer").getLastRow()-1;
// Fetch recent additions
var dataRange = ss.getSheetByName("Candidates Ready for Offer").getRange(startRow, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[5];
var message = "row[1]" + "row [2]" + "at" + "row[4]" + "is ready for an offer!"
var subject = "Candidate is Ready for Offer";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Thanks for sharing your sheet. There was actually a number of reasons why it wasn't working which I'll try and explain. Anyway, your script on the shared sheet will now function as you want it to. Here is what it looks like:
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("Candidates Ready for Offer");
var startRow = 4; // First row of data to process
var numRows = sh0.getLastRow()-startRow+1;
// Fetch recent additions
var dataRange = sh0.getRange(startRow,1,numRows,6);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
Logger.log(data);
for (i in data) {
var row = data[i];
var emailAddress = row[3];
Logger.log(emailAddress);
var message = "row[1]" + "row [2]" + "at" + "row[4]" + "is ready for an offer!"
var subject = "Candidate is Ready for Offer";
MailApp.sendEmail(emailAddress, subject, message);
}
}
There's no need to keep calling the same sheet when you've already defined it as the variable sh0
The data range was including the first 3 rows. You didn't want this. Some simple maths in the var numRows allows the spreadsheet to only take the range containing data starting at row 4.
You were referencing the wrong column for email address. It was in column 4.
I hope this helps/makes sense.
Oli
The problem is your var dataRange. At the moment your code is only getting the range of one cell (startrow,3) instead of an array.
Replacing .getRange(startrow,3) with getDataRange() should fix your issue.