简体   繁体   中英

Google Apps Script/Sheets - Script Function could not be found

I'm attempting to create a function in GAS and call it with a spreadsheet button. When I run it as follows it works with no issue:

function addButton() {
 var ss1 = SpreadsheetApp.getActiveSpreadsheet();
 var sheet1 = ss1.getSheets()[0];
 sheet1.getRange("A1").setValue(1)
}

and calling it on the spreadsheet with

addButton

However, what I'm trying to do is use variables to put different implementations of the function on different buttons. Here's the code I'm using currently:

function addButton(a,b) {
 var ss1 = SpreadsheetApp.getActiveSpreadsheet();
 var sheet1 = ss1.getSheets()[0];
 sheet1.getRange(a).setValue(b)
}

and then calling it on the spreadsheet to change value of cell A1 to 1 with

addButton("A1", 1)

But I'm getting the error

Script function addButton("A1", 1) could not be found

Unsure if this is a syntax thing or a particular quirk of Sheets. I'd think that if it were an error re: entering the wrong arguments format there would be a more helpful error message.

I don't know what's your issue exactly but this is working for me

function addButton(a,b) {
   var ss1 = SpreadsheetApp.getActiveSpreadsheet();
   var sheet1 = ss1.getSheets()[0];
   sheet1.getRange(a).setValue(b)
}

function test(){
  addButton("A1", 2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM