Google-sheets – Trouble renaming a pdf file created in Apps script

google sheetsgoogle-apps-scriptgoogle-forms

I used Google Forms to make a 20-question quiz and added a page to the response spreadsheet that automatically grades the quiz immediately after it's submitted. I have this script in place to automatically email me the graded quiz:

function sendEmails() {
    var emailAddress = "timlaird72@gmail.com";
    var file=DriveApp.getFileById('1ipTs_BEwXqurQUfcZ_QO1E110vw3Zv_06BQ7bLbdTkY');

    MailApp.sendEmail(emailAddress, 'Completed Test', 'Please Review the attached test.', {attachments: [file.getAs(MimeType.PDF)]});
}

I would like to rename the pdf file with the quiz takers name ('tim laird.pdf' instead of the current 'completed test.pdf'). The quiz takers first and last name are in cell A1 and B1 of the sheet being sent as the pdf.

Can anybody help with that?

Best Answer

Something like this will rename the file pulling from cells a1:b1

var file=DriveApp.getFileById('1ipTs_BEwXqurQUfcZ_QO1E110vw3Zv_06BQ7bLbdTkY');
      var range = SpreadsheetApp.getActive().getRange('A1:B1');
      var first = range.getCell(1, 1);
      var last = range.getCell(1,2);
      var name = first.getValue() + ' ' + last.getValue();

      file.setName(name);