How to change format of cell that runs function

后端 未结 2 1075
庸人自扰
庸人自扰 2021-01-16 03:26

If I have the following user defined function I can return the text \"foo\"...

function myFunction(input){
   return \"You wrote: \"+ input;
}
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 04:23

    If I'm following correctly you are calling the apps script function using formula syntax, by putting =myFunction() in a cell in your sheet.

    In this case, there is no script based method to apply formatting to the cell containing the formula. Not only are you not automatically given a reference to the parent cell in your function, you also do not have permission to call set methods (such as Range.setBackground()) from within a custom function call inside a cell formula.

    This is noted in the documentation under the Using Apps Script services heading:

    Spreadsheet: Read only (can use most get*() methods, but not set*()). Cannot open other spreadsheets (SpreadsheetApp.openById() or SpreadsheetApp.openByUrl()).

    https://developers.google.com/apps-script/guides/sheets/functions#advanced

    The only thing your custom function (when placed in a cell as an automatically executed formula) can do, is return either a string or array, which will then be displayed in the sheet.

提交回复
热议问题