Digital Farmer
Digital Farmer

Reputation: 2107

Call a script function from another function

I have two functions created in the same script and I would like the secondary to activate the main function if cell "N1" on the "Main Stage" page were written "Ok".


function CallFunction() {
  var spreadsheet = SpreadsheetApp.getActive();

  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Main Stage');
  var rg=sh.getRange("N1");
  var vA=rg.getValues();
  if (vA[0][0]=="Ok"){

  spreadsheet.getRange('Activate!A1').activate();
  spreadsheet.getCurrentCell().setFormula('=MainFunction()');
}

At the moment when trying to activate, it appears in the spreadsheet → You do not have the permission required to setValue (Line 10). From what I saw, I cannot activate my function directly from a spreadsheet cell.

How could I solve this problem?

Upvotes: 1

Views: 131

Answers (1)

Tanaike
Tanaike

Reputation: 201388

When you want to run the function of MainFunction() in the function of CallFunction(), please modify as follows. By this, MainFunction() can be run.

From:

spreadsheet.getCurrentCell().setFormula('=MainFunction()');

To:

MainFunction();

Note:

  • When =MainFunction() is used, unfortunately, setValue() cannot be used for the custom function.

Upvotes: 2

Related Questions