我写了一个函数,它有一个参数 - 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的方式/价值是什么,即你没有引用你认为自己的单元格或你获得该值的方式是错误的。 (这是最好的猜测,我可以拿出有限的代码,我的假设也可能完全错误:)) –