我寫了一個函數,它有一個參數 - updateBool
。該函數獲取調用單元的現有值。如果updatebool
爲是,則生成一個隨機數並作爲調用單元的新值返回。如果updateBool
爲否(即不需要更新),則返回單元的原始值。通過函數返回數字
當updateBool
爲yes時,該功能正確執行。但是,如果不是,返回的結果是「錯誤:結果不是數字」。
我改變了功能,所以我可以在調試模式下運行它 - 它似乎在此模式下正確執行,無論是yes和no參數都可以工作。但只要我通過updateBool
作爲參數(通過引用另一個單元格),它就會出錯。
function testFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var writeSheet = ss.getSheetByName('Data');
//for debug execution only
var callingRow = 4;
var callingColumn = '4';
updateBool = 'no'
//for when calling as function from spreadsheet
//callingRow = writeSheet.getActiveCell().getRow();
//callingColumn = writeSheet.getActiveCell().getColumn();
var callingCellRef = writeSheet.getRange(callingRow, callingColumn)
callingCellValue = Number(callingCellRef.getValue());
Logger.log('Calling Cell row: %s column: %s', callingRow, callingColumn);
Logger.log('Calling Cell Value: %s', callingCellValue);
if (updateBool == 'no') {
//DONT do any updates
Logger.log('UpdateBool is no, no update');
return callingCellValue;
}
else {
// do updates
var randomVariable = Math.random();
Logger.log('UpdateBool is yes, update to: %s', randomVariable);
return randomVariable;
}
}
的實際使用情況比這更復雜(我穿過多個參數和使用這些查詢數據庫),但是這是我不能獲得工作的邏輯。謝謝!
函數定義的值應該是'函數testFunction(updateBool){',對吧?並說'a1'是'7.77'。然後,在將'a1'設置爲'= testFunction(「something」)'的時刻,'a1'不再是'7.77',而是成爲一個公式('string')。所以在函數中,由於'a1'是一個字符串,它不能被轉換成數字。 –
我建議你保持數據與更新結果分離。 –
你能分享一下你如何得到updateBool的價值嗎?當值爲'yes'時,該函數正確運行,因爲它是函數中的默認邏輯。我相信問題在於你傳遞給updateBool的方式/價值是什麼,即你沒有引用你認爲自己的單元格或你獲得該值的方式是錯誤的。 (這是最好的猜測,我可以拿出有限的代碼,我的假設也可能完全錯誤:)) –