各位,有人能告诉我为什么这个代码在更新之前发送一个带有数据快照的pdf?
我在这里错过了什么?是否需要保存或刷新当前文件?感谢您的帮助。
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]});
}
- 您想知道
this code sends a pdf with a snapshot of data prior to the update
的原因
如果我的理解是正确的,这个答案怎么样?
修改要点:
- 在脚本中,使用
destRange.setValues(sourceRange.getValues());
将数据放入destRange
。虽然我不确定var sheetNumber = (tab.getIndex()-1);
的tab
,但如果您想将var destSheet = destFile.getSheetByName("dados do aluno");
的destSheet
导出为PDF,则需要将SpreadsheetApp.flush()
放在destRange.setValues(sourceRange.getValues());
之后。通过此操作,put值将反映到PDF数据中 - 从以上情况来看,关于
Is there a need to save or flush the current file?
,是肯定的
修改的脚本:
修改脚本时,请按如下方式进行修改。
发件人:destRange.setValues(sourceRange.getValues());
收件人:destRange.setValues(sourceRange.getValues());
SpreadsheetApp.flush(); // Added
参考:
- flush((
如果这不是你想要的方向,我道歉。