在定义时,您可以在无需实际调用气体的情况下定义功能吗?
我正在运行以下代码,并期望它在相关位置创建PDF。我已经单独测试了此EffortSpreadSheet功能,并且运行良好,但是当它带入该程序时,它不会发射。在执行日志中,它说该功能未定义(请参见下图)。
我是天然气的新手,但是有没有办法定义一个函数而又没有实际调用?
var ss = SpreadsheetApp.getActive()
var sheet1 = SpreadsheetApp.getActive().getSheetByName("Sheet1")
//Set active cell to A2 (cell below Name header) when file is opened.
function onOpen() {
sheet1.getRange('A2').activate();
}
//Look for name from scanner
function onEdit(e) {
var range = e.range;
if (range.getA1Notation() === 'A2') {
var active = ['Alpha', 'Beta'].includes(range.getValue()) ? "D2" : "A2"; //D2 for correct responses, A2 for incorrect (repeats the process)
sheet1.getRange(active).activate();
}
//Does this cell contain a valid part number?
var values = sheet1.getRange('D2:D27').getValues();
for(var i in values){
if(values[i][0].match("PLA-OPTRET1-")!=null){ //Retainer Element 1
//cell contains "PLA-OPTRET1-". Do something
range.offset(0,1).activate(); //In the Quantity column
}
if(values[i][0].match("PLA-OPT1-")!=null){ //Element 1
//cell contains "PLA-OPT1-". Do something
range.offset(0,1).activate();
}
if(values[i][0].match("Done")!=null){
function ExportSheet() {
const date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
var invMoves = SpreadsheetApp.openById('GenericSpreadsheetID').getSheetByName("GenericSheetName");
var lastbatch = invMoves.getValue('F2');
let inventoryTransaction = DriveApp.getFileById('GenericSpreadsheetId');
let blob = inventoryTransaction.getAs('application/pdf');
let pdf = DriveApp.getFolderById('NameOfPDFCreated')
.createFile(blob)
.setName(`Batch ${lastbatch} - Inventory Transaction ${date}`);
}
ExportSheet();
sheet1.getRange('F12').activate();
}
}
}
I am running the following code and was expecting it to create a pdf in the relevant location. I have tested this ExportSpreadsheet function on its own and it worked fine, but when it is brought in to this program, it does not fire. In the Executions log, it says the function is not defined (see image below).
I am new to GAS, but is there a way to define a function without actually calling it?
var ss = SpreadsheetApp.getActive()
var sheet1 = SpreadsheetApp.getActive().getSheetByName("Sheet1")
//Set active cell to A2 (cell below Name header) when file is opened.
function onOpen() {
sheet1.getRange('A2').activate();
}
//Look for name from scanner
function onEdit(e) {
var range = e.range;
if (range.getA1Notation() === 'A2') {
var active = ['Alpha', 'Beta'].includes(range.getValue()) ? "D2" : "A2"; //D2 for correct responses, A2 for incorrect (repeats the process)
sheet1.getRange(active).activate();
}
//Does this cell contain a valid part number?
var values = sheet1.getRange('D2:D27').getValues();
for(var i in values){
if(values[i][0].match("PLA-OPTRET1-")!=null){ //Retainer Element 1
//cell contains "PLA-OPTRET1-". Do something
range.offset(0,1).activate(); //In the Quantity column
}
if(values[i][0].match("PLA-OPT1-")!=null){ //Element 1
//cell contains "PLA-OPT1-". Do something
range.offset(0,1).activate();
}
if(values[i][0].match("Done")!=null){
function ExportSheet() {
const date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
var invMoves = SpreadsheetApp.openById('GenericSpreadsheetID').getSheetByName("GenericSheetName");
var lastbatch = invMoves.getValue('F2');
let inventoryTransaction = DriveApp.getFileById('GenericSpreadsheetId');
let blob = inventoryTransaction.getAs('application/pdf');
let pdf = DriveApp.getFolderById('NameOfPDFCreated')
.createFile(blob)
.setName(`Batch ${lastbatch} - Inventory Transaction ${date}`);
}
ExportSheet();
sheet1.getRange('F12').activate();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的错误,它说
dreversheetapp.spreadsheetid()
不是一个函数,因为它实际上不存在。您正在寻找 expraineSheetapp.openbyid() 。关于您的问题,当函数以这种方式定义时不会调用它们。您的呼叫发生在
exportsheet();
定义函数后。In regards to your error, it's saying
SpreadsheetApp.spreadsheetId()
is not a function because it doesn't actually exist. You're looking for SpreadsheetApp.openById().In regards to your question, functions are not called when they are defined in this manner. Your call happens at
ExportSheet();
after you've defined the function.