Google apps script 通过函数返回一个数字

Google apps script 通过函数返回一个数字,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我编写了一个只有一个参数的函数-updateBool。函数获取调用单元格的现有值。如果updatebool为yes,则生成一个随机数,并作为调用单元的新值返回。如果updateBool为否(即不需要更新),则返回单元格的原始值 当updateBool为是时,该函数将正确执行。但是,如果为“否”,则返回的结果是“错误:结果不是数字” 我更改了该函数,以便可以在调试模式下运行它-它似乎在该模式下正确执行,参数工作时是和否都正确。但是,只要我通过updateBool作为一个参数(通过引用另一个单元格)

我编写了一个只有一个参数的函数-
updateBool
。函数获取调用单元格的现有值。如果
updatebool
为yes,则生成一个随机数,并作为调用单元的新值返回。如果
updateBool
为否(即不需要更新),则返回单元格的原始值

updateBool
为是时,该函数将正确执行。但是,如果为“否”,则返回的结果是“错误:结果不是数字”

我更改了该函数,以便可以在调试模式下运行它-它似乎在该模式下正确执行,参数工作时是和否都正确。但是,只要我通过
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;
  }
}

实际用例比这更复杂(我正在传递多个参数并使用这些参数查询数据库),但这是我无法使用的逻辑。谢谢

这似乎很管用:

function testFunction () { 

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var writeSheet = ss.getSheetByName('Sheet1');  

  //for debug execution only
  var callingRow = 4;
  var callingColumn = '4';
  var updateBool = 'yes';


  //for when calling as function from spreadsheet
  //callingRow = writeSheet.getActiveCell().getRow();
  //callingColumn = writeSheet.getActiveCell().getColumn();

  var callingCellRef = writeSheet.getRange(callingRow, callingColumn);
  var callingCellValue = callingCellRef.getValue();

  callingCellValue = Number(updateBool,callingCellValue);

  Logger.log('Calling Cell row: %s column: %s', callingRow, callingColumn);
  Logger.log('Calling Cell Value: %s', callingCellValue);


}

function Number (updateBool,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;
  }

  }

D4值为
。请注意,此值是一个字符串

callingCellRef.getValue()
返回
no
,字符串的编号返回
NaN
,因此
callingCellValue
的值为
NaN

为了得到D4的变化值

callingCellValue = Number(callingCellRef.getValue());


函数定义应该是
Function testFunction(updateBool){
,对吗?并说
a1
7.77
。然后,当您将
a1
设置为
=testFunction(“某物”)
时,
a1
不再是
7.77
,而是成为一个公式(
string
)。因此,在函数中,由于
a1
是一个字符串,因此无法将其转换为数字。我建议您将数据与更新结果分开。您能否共享如何获取updateBool值的代码?当值为“是”时,函数会正确运行,因为这是函数中的默认逻辑。我相信问题出在这里关键在于你如何/什么样的值传递给updateBool,即你没有引用你认为你是的单元格,或者你获取该值的方式是错误的。(这是我能想出的最好的猜测,我的假设也可能是完全错误的:)@JagannathanAlagurajan调用单元格的值为“=testUpdateColumn(D4)”其中D4的值为“是”或“否”。D4的值可以是任何实际值-只需指示布尔值condition@SangbokLee-我确实考虑过这种可能性,这就是为什么我明确地把它转换成一个数,即:<代码> CalgEngalValue=数字(CalueCyrRe.GETValueE())谢谢,但我不认为这会有帮助。在原始代码中,我使用“Number”将值解析为数字类型。在这段代码中,Number函数无法实现这一点?我不明白它如何帮助返回值???
callingCellValue = callingCellRef.getValue();