0

I have a Google Form and Google Sheet set up to collect information I need. I currently have a script that will send me, and the form submitter, an email with a PDF attached that contains the contents of their form submission.

I am trying to edit that script and create another one that sends me a new version of the PDF after I go in and make a change to one of the cells associated with the original form submission (update the status of an issue, add notes, correct grammar, etc.).

This is what I have, I am still very new at programming and would appreciate any help...

(18 NOV @1425) It works! var last_column was not allowing the column after the edited cell to be defined. When I replaced last_column in var data with the actual number of columns that contained the data it worked great! Thank you to everyone who helped me figure this out, and learn a little along the way!

function onSheetEdit(e) {

    var source = SpreadsheetApp.getActiveSpreadsheet();
    var source_sheet = source.getSheetByName("Form Responses 1");
    var range = source_sheet.getDataRange();
    var ActiveRow = source_sheet.getActiveRange().getRow();
    var data = source_sheet.getRange(ActiveRow,1,1,4).getValues();

    var columnA = data[0][0];
    var columnB = data[0][1];
    var columnC = data[0][2];
    var columnD = data[0][3];

(18 NOV @0800) Another friend suggested I change the beginning to this... The email also sends, and now I am getting "1" "1" "/" and "1" in my four document placeholders...

function onSheetEdit(e) {

var source_sheet = e.source.getActiveSheet();
    if (source_sheet.getName() !== "Form Responses 1") return; //exit the script if edits are done on other sheets
    var data = source_sheet.getRange(e.range.rowStart, 1, 1, source_sheet.getLastColumn())//(StartRow,StartColumn,NumberofRowstoGet,NumberofColumnstoGet)
   .getValues()[0];
    Logger.log(data);

    var columnA = data[0][0];
    var columnB = data[0][1];
    var columnC = data[0][2];
    var columnD = data[0][3];

(17 NOV @1845) I had a friend help me from work and this is as far as we got... The email sends now, but the placeholders are not populating the data correctly in the PDF file attachment. It appears that the only data that is populating is data from row 1, and of that data, only the cell that was edited, and the data in the cells to the right of it...

function onSheetEdit(e) {

    var source = SpreadsheetApp.getActiveSpreadsheet();
    var source_sheet = source.getSheetByName("Form Responses 1");
    var range = source_sheet.getDataRange();
    var last_column = source_sheet.getActiveRange().getColumn();
    var ActiveRow = source_sheet.getActiveRange().getRow();
    var data = source_sheet.getRange(ActiveRow,1,1,last_column).getValues();

    var columnA = data[0][0];
    var columnB = data[0][1];
    var columnC = data[0][2];
    var columnD = data[0][3];

(16 NOV @1700) I edited the script to this but still no emails generated. I get this error emailed to me when script fails: "Cell reference out of range (line 13, file "Copy of Form confirmation emails")". Line 13 is "var row".

function onSheetEdit() {

    var source = SpreadsheetApp.getActiveSpreadsheet();
    var source_sheet = source.getActiveSheet()
    var row = source_sheet.getActiveCell().getRow();
    var last_column = source_sheet.getLastColumn();
    var data = source_sheet.getRange(row,1,1,last_column).getValues();

    var columnA = data[0][0];
    var columnB = data[0][1];
    var columnC = data[0][2];
    var columnD = data[0][3];

(16 NOV @ 1330) I tried this instead but still no emails generated...

function onSheetEdit() {

    var source = SpreadsheetApp.getActiveSpreadsheet();
    var source_sheet = source.getActiveSheet()
    var row = source_sheet.getActiveCell().getRow();
    var last_column = source_sheet.getLastColumn();
    var data = source_sheet.getRange(row,1,1,last_column);

    var columnA = data.values[0];
    var columnB = data.values[1];
    var columnC = data.values[2];
    var columnD = data.values[3];

Original script...

function onSheetEdit(e) {

    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var rows = sheet.getActiveCell().getRow();

    var columnA = e.values[0];
    var columnB = e.values[1];
    var columnC = e.values[2];
    var columnD = e.values[3];      


    var docTemplate = "1WyWeCLQQ3en1EbKjOLcWxlOLc0fHHDpZrB9yfXZ7nv8";
    var docName = "Test form script";
    var carbonCopyEmail = "jeffery.crane@goarmy.com";
    var submitterEmail = columnB;
    var dataName = columnC; 
    var submitDate = columnA;
    var attachmentName = docName + ' for data ' + dataName

    var submitterEmailPlaceholder = 'keyUsername';
    var submitDatePlaceholder = 'keyTimestamp';
    var templatePlaceholder1 = 'keyQuestion1';
    var templatePlaceholder2 = 'keyQuestion2';

    var submitterSubject = "Test Script Confirmation Email for data " + dataName;
    var submitterBody = "Attached is a PDF confirmation sheet with the details of your submission of data: " + dataName + " submitted on " + submitDate;
    var carbonCopySubject = "Test Script Submission Notification Email for data " + dataName;
    var carbonCopyBody = "Attached is a PDF confirmation sheet with the details of " + submitterEmail + "'s submission of data: " + dataName + " on " + submitDate;





    //Gets document template defined by the docID above, copys it as a new temp doc, and saves the Doc’s id
    var copyId = DocsList.getFileById(docTemplate)
    .makeCopy(attachmentName)
    .getId();
    //Open the temporary document
    var copyDoc = DocumentApp.openById(copyId);
    //Get the document’s body section
    var copyBody = copyDoc.getActiveSection();

    //POSSIBLE MODIFICATION TO ADD LINES OF CODE
    //Replace place holder keys with the spreadsheet values in the google doc template
    //This section of the script looks for instances where the key appears in the Google Doc and replaces the instance 
    //with the defined variable
    //For instance, whenever "keyUserName" (defined above as submitterEmailPlaceholder) appears in the Google Doc, 
    //the value from the spreadsheet in columnB replaces "keyUserName" 
    copyBody.replaceText(submitDatePlaceholder, columnA);
    copyBody.replaceText(submitterEmailPlaceholder, columnB);
    copyBody.replaceText(templatePlaceholder1, columnC);
    copyBody.replaceText(templatePlaceholder2, columnD);

    //Save and close the temporary document
    copyDoc.saveAndClose();

    //Convert temporary document to PDF
    var pdf = DocsList.getFileById(copyId).getAs("application/pdf");

    //Attaches the PDF and sends the email to the form submitter
    MailApp.sendEmail(submitterEmail, submitterSubject, submitterBody, {htmlBody: submitterBody, attachments: pdf});

    //Attaches the PDF and sends the email to the recipients in copyEmail above
    MailApp.sendEmail(carbonCopyEmail, carbonCopySubject, carbonCopyBody, {htmlBody: carbonCopyBody, attachments: pdf});

    //Deletes the temporary file
    DocsList.getFileById(copyId).setTrashed(true);
}
MSG Jeff Crane
  • 79
  • 3
  • 13
  • 1
    What is not working in this code? At first sight it seems to be OK...can you point the issue you have? – Serge insas Nov 15 '14 at 20:52
  • Serge, I am getting this error: TypeError: Cannot read property "values" from undefined. It is highlighting var columnA. I know for sure that everything under the e.values works, because it works in my script to generate the documents when a form is submitted. The part I need help with is figuring out how to define all of the data in the row that is changed, when a cell is changed. – MSG Jeff Crane Nov 15 '14 at 21:50
  • Normal... please refer to this post to see how you can test the code in the script editor: http://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas – Serge insas Nov 15 '14 at 21:54
  • Still no luck... How can I help you help me better? Do I share the file with you? – MSG Jeff Crane Nov 16 '14 at 05:42
  • Itb seems like I am missing something. My gut tells me that I have to do something with var rows, but again, I am new at this... – MSG Jeff Crane Nov 16 '14 at 16:55
  • @Sergeinsas I added another attempt to format the data above, but still no luck... – MSG Jeff Crane Nov 16 '14 at 18:41
  • You cannot do data.values, data is a range object, you should do getValues() to get an array of values: data[row][col] ` var data = source_sheet.getRange(row,1,1,last_column).getValues() var columnA = data[0][0]; var columnB = data[1][0]; var columnC = data[2][0]; var columnD = data[3][0]; ` – Riël Nov 16 '14 at 20:10
  • @SFCCrane- sorry not much time these days... – Serge insas Nov 16 '14 at 21:11

2 Answers2

1

You cannot do data.values, data is a range object, you should do getValues() to get an array of values: data[row][col]

 var data = source_sheet.getRange(row,1,1,last_column).getValues()

var columnA = data[0][0];
var columnB = data[1][0];
var columnC = data[2][0];
var columnD = data[3][0];
Riël
  • 1,251
  • 1
  • 16
  • 31
  • Thanks @Reil , I included that and set a notification in the trigger on script failure. The email I received says: "Cell reference out of range (line 13, file "Copy of Form confirmation emails")". Line 13 is: "var row = source_sheet.getActiveCell().getRow();" – MSG Jeff Crane Nov 16 '14 at 20:55
  • 1
    @Riel - small confusion : first index is row, second is column >> your example is column-row in the wrong order... distraction I'm sure :-) – Serge insas Nov 16 '14 at 21:09
  • @riel I changed the code as suggested but I am still receiving the Line 13 (var row) error... – MSG Jeff Crane Nov 16 '14 at 22:04
  • It should be working. Are you shure the last_column value is not 0? There should be a column length. – Riël Nov 17 '14 at 17:13
  • @SFCCrane Your first code (**16 NOV @1700**) is correct. There is nothing wrong in that. I don't understand why are you getting errors at `var row?` – rpm Nov 17 '14 at 21:58
  • @user1989 just updated with current code and notes, we got the email to work, but now having issues with the proper data populating the PDF attachment – MSG Jeff Crane Nov 17 '14 at 23:49
  • @Riel just updated with current code and notes, we got the email to work, but now having issues with the proper data populating the PDF attachment – MSG Jeff Crane Nov 17 '14 at 23:49
  • @Sergeinsas just updated with current code and notes, we got the email to work, but now having issues with the proper data populating the PDF attachment – MSG Jeff Crane Nov 17 '14 at 23:50
  • I definitely have an issue with the data definition because the email body (var carbonCopyBody) states "Attached is a PDF confirmation sheet with the details of the modification to undefined's submission of data: undefined on Timestamp". Basically, I believe that variables columnA thru columnD are not being mapped correctly???? – MSG Jeff Crane Nov 17 '14 at 23:54
  • @SFCCrane Because I guess your doc must be empty right now, and it won't be containing these strings `"keyUserName", "keyTaskName", "keyQuestion1", "keyQuestion2"` in your doc. So , your code is trying to replace these strings, but if it won't be in your doc, your code won't replace any strings, it will return empty doc and will generate empty PDF. So first, add these strings in your doc, and test your code. I believe it will run as you would have wanted. – rpm Nov 18 '14 at 00:06
  • @user1989 the strings are in there, I use the same doc template to generate a similar email when the initial form submission is made. – MSG Jeff Crane Nov 18 '14 at 00:40
  • @SFCCrane Again, I don't understand why is not working for you while it's working for me perfectly without any modification in your code? It also sends me PDF with modified values. I would suggest to debug your code for once, so you will get an idea where is the exact issue? – rpm Nov 18 '14 at 01:02
  • @user1989, I have run the debugger, nothing comes up. If I shared the document with you, would you mind looking at it? – MSG Jeff Crane Nov 18 '14 at 01:20
  • @SFCCrane Sure. You can share the link of screenshot of your document or share the public link of your document here. – rpm Nov 18 '14 at 01:50
  • @user1989 here is the link: https://drive.google.com/folderview?id=0B5YzIHuPSKw1WWMwbW92SGVtLUU&usp=sharing – MSG Jeff Crane Nov 18 '14 at 02:05
  • @user1989, the problem is that you will be unable to submit a form because it is restricted to our domain. Should not be a problem because this script is for when the resulting spreadsheet data is modified... Feel free to modify any cell you need – MSG Jeff Crane Nov 18 '14 at 02:07
  • @SFCCrane It doesn't work because strings in your template are different than in your code, and remember string are case sensitive here. Make these Changes in your test template like `keyTimestamp to keyTaskName and keyUsername to keyUserName`. Make these two changes in your doc. You are good to go. – rpm Nov 18 '14 at 17:54
  • @user1989 not sure how those placeholders got changed, probably during the troubleshooting frenzy! I updated the original post with what works now, and I am receiving the correct doc and information! Thank you to everyone who helped me, I really appreciate it! – MSG Jeff Crane Nov 18 '14 at 19:31
0

Why instead of mess manually with the cells values of the responses sheet you don't use the form edit link? You can put it into the mail you are already sending.

You can get it from:

var editLink = lastResponse.getEditResponseUrl(); 

When the form is submitted again from the edit link the script you wrote script will be run again sending the updated PDF.

Kintaro
  • 178
  • 3
  • 14