2017-01-02 56 views
1

我正在嘗試在Google表格中運行自定義功能。我沒有得到一個編譯錯誤,但是當我嘗試調用的函數,我得到一個#ERROR與消息一起,Google腳本錯誤:「您無權調用排序」

You do not have permission to call sort

有沒有辦法來解決這個問題呢?如果range.sort不可用,如何在自定義函數中對範圍進行排序?

這裏是我的代碼:

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; 

} 
+0

您的腳本在我的電子表格中工作。你有編輯電子表格的權限嗎?有一件事你可能想要解決:var sheet = SpreadsheetApp.getActiveSpreadsheet();將一個電子表格對象分配給變量'sheet',這是令人困惑的(並且使得它不清楚後續操作在哪個表單上執行)。它應該可能是'var sheet = SpreadsheetApp.getActiveSheet();' – FTP

+0

試試這個:'sheet.getRange(「C3:C9」)。setValues(sheet.getRange(「C3:C9」)。getValues()。sort ));'在這裏,我假設你想在C3:C9範圍內對值進行排序,並在電子表格中顯示它。 –

回答

1

正如documentation規定:

A custom function cannot affect cells other than those it returns a value to. In other words, a custom function cannot edit arbitrary cells, only the cells it is called from and their adjacent cells. To edit arbitrary cells, use a custom menu to run a function instead.

你的函數試圖修改從起源細胞不同的範圍,解釋你的錯誤newRange對象