简体   繁体   中英

How to get the cell values (Sheet1 from Google Form, Sheet2 from Formula) to google doc template

I have 2 sheets. Sheet1 is from Google Form and query them to Sheet2 then make the formula. I would like to get the cell values from them to Google Doc template and send the email. I can get values from Sheet1 but can't from Sheet2. Please help me solve this problem. Thank you. enter image description here enter image description here

 function onFormSubmit(e){ var docs_email = e.values[1]; var Name = e.values[2]; var Date1 = e.values[3]; var Date2 = e.values[4]; var Money1 = e.values[5]; var Money2 = e.values[6]; //Values from Sheet2 var TotalDate = e.values[7]; var Money3 = e.values[8]; var TotalMoney = e.values[9]; var copyId = DriveApp.getFileById('Doc ID') //Temp document ID.makeCopy('Report-'+Name).getId(); var copyDoc = DocumentApp.openById(copyId); copyDoc.getBody() var copyBody = copyDoc.getActiveSection(); copyBody.replaceText('keyName', Name); copyBody.replaceText('keyDate1', Date1); copyBody.replaceText('keyDate2', Date2); copyBody.replaceText('keyMoney1', Money1); copyBody.replaceText('keyMoney2', Money2); copyBody.replaceText('keyTotalDate', TotalDate); copyBody.replaceText('keyMoney3', Money3); copyBody.replaceText('keyTotalMoney', TotalMoney); copyDoc.saveAndClose(); //convert to pdf var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); //Send the email var subject = "Your Report"; var body = "Dear " +Name+" \n\n"+"<br> This is your report"; GmailApp.sendEmail(docs_email, subject, body,{htmlBody: body, attachments: pdf}); }

How about this answer?

Modification points:

  • In order to retrieve the values from the columns "H" to "J" in "Sheet2", getValues is used.
  • From your question, I could understand that the columns "H" to "J" have the formula. So I thought that getLastRow() might not be able to be directly used. So I thought that it is required to retrieve the 1st empty row at the column "A".

When above points are reflected to your script, it becomes as follows.

Modified script:

Please modify your script as follows.

From:
 var TotalDate = e.values[7]; var Money3 = e.values[8]; var TotalMoney = e.values[9];
To:
 SpreadsheetApp.flush(); // This might be not required. var sheet = e.source.getSheetByName("Sheet2"); // Values are retrieved from the sheet name of "Sheet2". var row = 0; for (var i = 1; i < sheet.getLastRow(); i++) { if (sheet.getRange(i, 1).isBlank()) { row = i - 1; break; } } var [TotalDate, Money3, TotalMoney] = sheet.getRange(row, 8, 1, 3).getValues()[0];

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM