Folks, Anybody can tell me why this code sends a pdf with a snapshot of data prior to the update?
What am I missing here? Is there a need to save or flush the current file? Any help is appreciated here.
Tks, Ivan Daudt
function onFormSubmit(e) {
var responses = e.namedValues;
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var row = s.getActiveCell().getRow(); //identify last row
var sourceRange = s.getRange(row,1,1,38); //Get the soure range.
//open target file and updates its data
var destFile = SpreadsheetApp.openById("1c8NepY_2ZDxrRVzrhbax4VRPGarqb59eir8wNQ7w4qc");
SpreadsheetApp.setActiveSpreadsheet(destFile);
var destSheet = destFile.getSheetByName("dados do aluno");
var destRange = destSheet.getRange(2,1,1,38); //Gets the destination sheet range.
destRange.setValues(sourceRange.getValues()); //Gets source sheet value and uses it to set the imported sheet value.
//send by email
var sheetNumber = (tab.getIndex()-1);
var sheetId = sheetNumber ? destFile.getSheets()[sheetNumber].getSheetId() : null;
var url_base = destFile.getUrl().replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + destFile.getId()))
// following parameters are optional...
+ '&size=A4' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=true' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var thePdf = response.getBlob().setName('Avaliação '+ onome + '.pdf');
MailApp.sendEmail(
mailtoaddr[0],
subject,
message,
{attachments: [thePdf]});
}