在定义时,您可以在无需实际调用气体的情况下定义功能吗?

发布于 2025-02-11 14:08:12 字数 2035 浏览 1 评论 0原文

我正在运行以下代码,并期望它在相关位置创建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();

    }

    }
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2025-02-18 14:08:12

关于您的错误,它说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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文