[英]Script generating pdf with old data snapshot from google spreadsheet?
Folks, Anybody can tell me why this code sends a pdf with a snapshot of data prior to the update?伙计们,谁能告诉我为什么此代码在更新之前发送带有数据快照的pdf?
What am I missing here?我在这里缺少什么? Is there a need to save or flush the current file?
是否需要保存或刷新当前文件? Any help is appreciated here.
任何帮助在这里表示赞赏。
Tks, Ivan Daudt Tks,伊万·道特
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
.this code sends a pdf with a snapshot of data prior to the update
的原因。 If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样?
destRange
using destRange.setValues(sourceRange.getValues());
destRange.setValues(sourceRange.getValues());
将数据放入destRange
destRange.setValues(sourceRange.getValues());
. tab
of var sheetNumber = (tab.getIndex()-1);
var sheetNumber = (tab.getIndex()-1);
tab
, if you want to export destSheet
of var destSheet = destFile.getSheetByName("dados do aluno");
destSheet
的var destSheet = destFile.getSheetByName("dados do aluno");
as a PDF, it is required to put SpreadsheetApp.flush()
after destRange.setValues(sourceRange.getValues());
SpreadsheetApp.flush()
放在destRange.setValues(sourceRange.getValues());
. Is there a need to save or flush the current file?
Is there a need to save or flush the current file?
, it's yes. When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。
destRange.setValues(sourceRange.getValues());
To:
destRange.setValues(sourceRange.getValues()); SpreadsheetApp.flush(); // Added
If this was not the direction you want, I apologize.如果这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.