-1

Hi Everyone I am trying to use "Importrange" function in a custom function (shown below).

function csvLinks(sheetName,range) 
{

    var link1 ="\"https://docs.google.com/spreadsheets/d/1Gnqa4WOC7mRX2meqwrUCrh9dcS94mI0G-abc123\"";

    var link2 = "\""+sheetName+"!"+range+"\"";

    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var sheet = ss.getSheets()[0];

    var cell = sheet.getRange("A1");

    return cell.setFormula("=IMPORTRANGE(link1,link2)");
}

However when I try use this I get the below error messages.

Cell A1 Error = You do not have permission to call setFormula (line 7).

Cell A2 Error = Unknown range name: 'LINK1'

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Possible duplicate of [Google Script setValue permission](https://stackoverflow.com/questions/15933019/google-script-setvalue-permission) – Rubén Jun 17 '19 at 16:44

1 Answers1

1

Based from this thread,

[Source.] 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]];.

Your error can mean that simple triggers can't do anything that requires authorization but installable ones do. From this forum, it stated that custom functions can only modify the cell from which they are called, they cannot modify arbitrary cells.

You can also check these related threads:

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59