Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

google apps script - Custom function constantly throwing an error

I wrote a custom in-cell function for having a specified data validation list only if a given cell isn't empty. Here's that function:

function CONDITIONALVALIDATION(sheetName,cellToCheckA1,validationCell,validationItems){
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName(sheetName);
  var cellValue = sheet.getRange(cellToCheckA1).getValue();
  var cellToSet = sheet.getRange(validationCell);
  cellToSet.clearDataValidations();
  if(cellValue!=""){
  var unitsRule = SpreadsheetApp.newDataValidation().requireValueInList(validationItems, true);
    cellToSet.setDataValidation(unitsRule)
  }
}

When I call a test function which fills the parameters for the CONDITIONALVALIDATION function and run it within the editor, I see the desired results. That function is the following:

function testconditional(){
  CONDITIONALVALIDATION("Front End","E12","F12",["x","y"]);
}

However, when I call the function as a custom in-cell function, I constantly get a

"Formula Parse Error."

I have already ruled out syntax as a possible cause; the call to the function in my test function is the exact same as when I call it within a custom in-cell function. Save, of course, the equals sign before the function call to indicate that it is a function to be run. I also know that the function is recognized by sheets; there is no "Function nonexistant", or "#NAME?" error when the function is called within the cell.

How do I solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Issue(s):

Javascript Array literals [] are not valid spreadsheet formula syntax. Try {} instead:

=CONDITIONALVALIDATION("Front End","E12","F12",{"x","y"});

Having said that, As written in the documentation, Custom functions can't .setDataValidation(unitsRule)

Spreadsheet

Read only (can use most get*() methods, but not set*()).

Solution:

To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...