AppScript:调用的函数在放入循环时不返回值作为参数?
嗨,stackoverflow 的所有伟大人士,当我在“paySlipMethod()”函数末尾记录“paySlip”变量时,会检索 URL。然而,当我在循环中调用“generatePaySlips()”函数中的函数时,我得到“null”,更不用说将 URL 推入“links”数组中了......请赐教,十亿感谢你们所有人。
function generatePaySlips(paySlip){
const month = currentSheet.getRange("B1").getDisplayValues();
const year = currentSheet.getRange("C1").getDisplayValues();
const data = currentSheet.getRange(3,2,currentSheet.getLastRow()-2,21).getDisplayValues();
let errors = [],links = [];
data.forEach(row => {
try{
paySlipMethod (month,year,row[0],row[1],row[2]...blabla,paySlipTemp,tempFolder,pdfFolder,paySlip);
errors.push(["OK"]);
links.push([paySlip]);
Logger.log(paySlip);
} catch(err){
errors.push(["error"]);
}
});
currentSheet.getRange(3,22,currentSheet.getLastRow()-2,1).setValue(errors);
currentSheet.getRange(3,23,currentSheet.getLastRow()-2,1).setValue(links);
function paySlipMethod(month,year,eng_name....paySlipTemp,tempFolder,pdfFolder,paySlip) {
const tempSlip = paySlipTemp.makeCopy(tempFolder);
const tempPaySlip = DocumentApp.openById(tempSlip.getId());
const body = tempPaySlip.getBody();
body.replaceText("{month}",month);
blabla...the rest
tempPaySlip.saveAndClose();
const pdfContentBlob = tempSlip.getAs(MimeType.PDF);
var paySlip = pdfFolder.createFile(pdfContentBlob).setName(month+" "+year+" - "+eng_name).getUrl();
tempFolder.removeFile(tempSlip);
return paySlip;
}
Hi all great minds of stackoverflow, when I log "paySlip" variable at the end of "paySlipMethod()" function, the URLs are retrieved. However, when I call the function in "generatePaySlips()" function in a loop, I get "null", let alone pushing the URLs into "links" array... please enlighten me, a billion thanks to you all.
function generatePaySlips(paySlip){
const month = currentSheet.getRange("B1").getDisplayValues();
const year = currentSheet.getRange("C1").getDisplayValues();
const data = currentSheet.getRange(3,2,currentSheet.getLastRow()-2,21).getDisplayValues();
let errors = [],links = [];
data.forEach(row => {
try{
paySlipMethod (month,year,row[0],row[1],row[2]...blabla,paySlipTemp,tempFolder,pdfFolder,paySlip);
errors.push(["OK"]);
links.push([paySlip]);
Logger.log(paySlip);
} catch(err){
errors.push(["error"]);
}
});
currentSheet.getRange(3,22,currentSheet.getLastRow()-2,1).setValue(errors);
currentSheet.getRange(3,23,currentSheet.getLastRow()-2,1).setValue(links);
function paySlipMethod(month,year,eng_name....paySlipTemp,tempFolder,pdfFolder,paySlip) {
const tempSlip = paySlipTemp.makeCopy(tempFolder);
const tempPaySlip = DocumentApp.openById(tempSlip.getId());
const body = tempPaySlip.getBody();
body.replaceText("{month}",month);
blabla...the rest
tempPaySlip.saveAndClose();
const pdfContentBlob = tempSlip.getAs(MimeType.PDF);
var paySlip = pdfFolder.createFile(pdfContentBlob).setName(month+" "+year+" - "+eng_name).getUrl();
tempFolder.removeFile(tempSlip);
return paySlip;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解您的要求,您希望将从
paySlipMethod
返回的 URL 推送到links
数组中。但我在这里看不到任何存储返回 URL 的变量:-
尝试此修改:-
这里
var paySlip
将存储返回的URL
,您可以稍后使用。As much i understand your requirement you want to push the URL returned from
paySlipMethod
intolinks
array.But I can't see any variable which is storing the returned URL here:-
Try this modification :-
Here
var paySlip
will store the returnedURL
which you can use later.