Custom functions can not be used with services that require authorization.
All Set
methods belong to this category, in your case setNumberFormat
:
See the official documentation regarding custom functions:
If your custom function throws the error message You do not have
permission to call X service., the service requires user authorization
and thus cannot be used in a custom function.
To use a service other than those listed above, create a custom menu
that runs an Apps Script function instead of writing a custom
function. A function that is triggered from a menu will ask the user
for authorization if necessary and can consequently use all Apps
Script services.
Alternatively, you can run the function from the script editor but keep in mind that you need to pass the arguments to be able to execute it or define them within your function.
In this case, since you are not using value
and currency
within the FORMATCURRENCY
function, you can execute this directly from your script editor or from a custom menu but not as a formula in your sheet:
function FORMATCURRENCY() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("D19");
range.setNumberFormat("$#,##0.00;$(#,##0.00)");
}