0

I need to create a custom function in Google Apps Script that will allow me to input the location of certain cells and then the function would output into new cells. This is what I tried sofar:

function SetRange(RowNum) {
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  sheet.getActiveCell(),setValue(45)
  sheet.getRange(RowNum,24),setValue(51);

  }

I get the error that "SetValue is not defined. I am building this program as I learn so there are some facts that veteran programmers would know that I do not. Thanks again for all your help

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • It should be `.setValue(45)` not `,setValue(45)`. Note the use of period rather than comma. – Jack Brown Jan 26 '18 at 16:23
  • I fixed that and got the following:Error You do not have permission to call setValue (line 6). – Charlie Cannon Jan 26 '18 at 16:31
  • 1
    I see you are writing a custom function! Custom functions can only modify the cell they are being called from. More details [here](https://developers.google.com/apps-script/guides/sheets/functions#return_values) – Jack Brown Jan 26 '18 at 16:41

1 Answers1

1

Custom functions could return an array of values but can't use setValue() or setValues().

Example

function demo(){
  var output = [
    [1,2],
    [3,4]
  ]
  return output;
}

If we add =demo() to A1, the result will be:

  |  A  |  B  |
--+-----+-----+
 1|    1|    2|
 2|    3|    4|
Rubén
  • 34,714
  • 9
  • 70
  • 166