Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom function throws a "You do not have the permission required to setValue" error

I am trying to set some value to a cell in a Google Spreadsheet:

    function exampleFunction() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheets()[0];
      var range1 = sheet.getRange("A1");
      var value1 = range1.getValue();
      value1+=1;
      range1.setValue(2);
      return value1;
    }

If I am trying to affect a cell with this function, this error appears:

You do not have the permission required to setValue. (line 10, file "ddd")

Do you know how I could make that possible? I actually want the affected cell to take the value of the cell A1 and increase the value of A1 by 1.

like image 856
zyrup Avatar asked Apr 10 '13 17:04

zyrup


4 Answers

from the documentation :

Custom functions return values, but they cannot set values outside the cells they are in. In most circumstances, a custom function in cell A1 cannot modify cell A5. However, if a custom function returns a double array, the results overflow the cell containing the function and fill the cells below and to the right of the cell containing the custom function. You can test this with a custom function containing return [[1,2],[3,4]];.

reference : Custom Functions in Spreadsheets

like image 64
Serge insas Avatar answered Oct 13 '22 07:10

Serge insas


It looks that you are using the above function as a custom function, in other words, it is called by cell formula on the Google Sheets UI, in the following way:

=exampleFunction()

Custom functions in Google Sheets have limitations like they can't be used to call Google Apps Script services that require permissions. The workaround is to use another mean to call the function:

  • Run it from the Google Apps Script Editor
  • Use a custom menu
  • Use a trigger

Also they could be called from dialogs and sidebars, Google Apps Script Web apps and by using the Google Apps Script execution API

like image 42
Rubén Avatar answered Oct 13 '22 08:10

Rubén


It's just a little different than what we programmers think.
You can use setFormula in a Macro but not in a custom function.
Just create a simple macro from Tools > Macros > Record Macro, and then open the Script editor and change the Macro's code to your code...

Here is my Macro's code:

function SetFormula() {
  var spreadsheet = SpreadsheetApp.getActive();
  var formulaValue = spreadsheet.getRange('formulaText').getValue().toString();
  spreadsheet.getRange('total').setFormula(formulaValue);
  return formulaValue;
};

Then, to run your macro automatically (you can run that manually from Tools > Macros > YOUR-MACRO-NAME), just create a trigger as follows:

  1. Open the Script Editor:

Script Editor

  1. Then go to Triggers from the left side panel and tap on Add Trigger button:

Triggers item in the left side panel

Triggers page

  1. Finally, create the trigger, select your Macro from the list (mine is SetFormula), select the Event Source as From SpreadSheet, the Event Type to On Edit, and save it.

Create a new trigger

That's it!


I named my ranges as FormulaText and total to be more flexible.
you can do that from here:

Naming a range

like image 2
Dr TJ Avatar answered Oct 13 '22 08:10

Dr TJ


Custom functions do have permission limitations as noted above. They can run with a custom menu or you can insert an image and assign a custom script to it to use it like a button.

Using a Trigger is another way to accomplish something like this example, which makes it automatic.

A simple trigger in an App Script such as onSelectionChange(e) works without running into the permissions issue of putting a custom function into a cell. This trigger is newer than what was available in the original post. In the simple example below, cell A1 will turn white with an even integer and red with anything else. Granted, the speed at which the triggers fire may vary. It's not always as instantaneous as one might expect.

function onSelectionChange(e) {

  const sheet = SpreadsheetApp.getActive()
  var value1 = sheet.getRange("A1").getValue()
    if(value1 % 2 == 0) {
        sheet.getRange("A1").setBackground("#FFFFFF") //white
    } else {
        sheet.getRange("A1").setBackground("#FF0000") //red
    }
}
like image 1
Chris3643 Avatar answered Oct 13 '22 07:10

Chris3643