javascriptgoogle-apps-scriptgoogle-sheetsgoogle-sheets-formulagoogle-sheets-custom-function

setValue() or setFormula() with complex formulas/"You do not have permission to call setFormula"


So I keep getting this error message in my spreadsheet when I try to run my code in google script:

You do not have permission to call setFormula.

I tried with both setValue() and setFormula() and the error is the same. I saw somewhere that setFormula() only works with simple formulas (ex. A2*B2/C2).

This is a simple function to concat a hyperlink to the content of the selected cell and forma a hyperlink sintax.

function HIPERLINK() {
      var ws = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var cell = ws.getActiveCell();
      var auto = cell.getValue();
      var link = '=HIPERLINK("https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=pesquisaSimples&-H&Host:&projudi.tjpr.jus.br&-H&User-Agent:&Mozilla/5.0&(Windows&NT&6.3;&WOW64;&rv:49.0)&Gecko/20100101&Firefox/49.0&-H&Accept:&text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&-H&Accept-Language:&pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3&--compressed&-H&Referer:&https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=iniciarSimples&-H&Cookie:&projudiContCookie=0;&JSESSIONID=053165f8dd5f8532c326f3eb06d7;&projudi-route=4;&dtLatC=54;&dtPC=-;&dtCookie=49542FA50EF89B032E8685F08394F120|UHJvanVkaSstK0V4dGVybm98MQ&-H&Connection:&keep-alive&-H&Upgrade-Insecure-Requests:&1&--data&page=1&flagNumeroUnico=true&flagNumeroFisicoAntigo=false&numeroProcesso='
        +auto+'";"'+auto+'")';
      cell.setFormula(link);
 }

If anyone knows a way to do this without the error message, thanks.


Solution

  • I tried this way and it worked for me without issues:

    function setCustomLink(){
      var ss = SpreadsheetApp.getActive().getActiveSheet();
      var cell = ss.getActiveCell();
      var cellValue = cell.getValue();
      cell.setValue("https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=pesquisaSimples&-H&Host:&projudi.tjpr.jus.br&-H&User-Agent:&Mozilla/5.0&(Windows&NT&6.3;&WOW64;&rv:49.0)&Gecko/20100101&Firefox/49.0&-H&Accept:&text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&-H&Accept-Language:&pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3&--compressed&-H&Referer:&https://projudi.tjpr.jus.br/projudi/processo/buscaProcesso.do?actionType=iniciarSimples&-H&Cookie:&projudiContCookie=0;&JSESSIONID=053165f8dd5f8532c326f3eb06d7;&projudi-route=4;&dtLatC=54;&dtPC=-;&dtCookie=49542FA50EF89B032E8685F08394F120|UHJvanVkaSstK0V4dGVybm98MQ&-H&Connection:&keep-alive&-H&Upgrade-Insecure-Requests:&1&--data&page=1&flagNumeroUnico=true&flagNumeroFisicoAntigo=false&numeroProcesso=" + cellValue + ";" + cellValue);
    }
    

    Let me know if you get different result.

    Tip: you can create a button and assign setCustomLink function to it, and when you need reformatting just click the button.

    Here is the link how to accomplish this: Clickable images and drawings in Google Sheets.