I am trying to run a custom function within Google Sheets. I do not get a compile error but when I try to call the function, I get an #ERROR along with a message,
You do not have permission to call sort
Is there a way to get around this problem? How can I sort a range within a custom function if range.sort
is not available?
Here is my code:
function installmentPrice(priceRange, measuringPeriod, lowestDays, discount)
{
measuringPeriod = measuringPeriod || 5;
lowestDays = lowestDays || 1;
discount = discount || 1;
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var tempRange = sheet.getRange("C3:C9");
var newRange = tempRange.offset(0,0,measuringPeriod);
newRange.sort(newRange.getColumn());
Logger.log(newRange.getValues());
var lowestSum = 0;
var installmentPx = 0;
for (i=0; i<= lowestDays; i++) {
lowestSum = lowestSum + newRange[i];
}
lowestSum = lowestSum/lowestDays;
installmentPx = discount * lowestSum;
return installmentPx;
}
var sheet = SpreadsheetApp.getActiveSpreadsheet();
assigns a spreadsheet object to variablesheet
, which is confusing (and makes it unclear what sheet the subsequent operations are performed on). It should probably bevar sheet = SpreadsheetApp.getActiveSheet();
- user6655984sheet.getRange("C3:C9").setValues(sheet.getRange("C3:C9").getValues().sort());
Here, I'm assuming that you want to sort values in C3:C9 range and show it on spreadsheet itself. - Shyam Kansagra