2017-02-13 92 views
1

我写了一个函数,它有一个参数 - 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; 
    } 
} 

的实际使用情况比这更复杂(我穿过多个参数和使用这些查询数据库),但是这是我不能获得工作的逻辑。谢谢!

+0

函数定义的值应该是'函数testFunction(updateBool){',对吧?并说'a1'是'7.77'。然后,在将'a1'设置为'= testFunction(“something”)'的时刻,'a1'不再是'7.77',而是成为一个公式('string')。所以在函数中,由于'a1'是一个字符串,它不能被转换成数字。 –

+0

我建议你保持数据与更新结果分离。 –

+0

你能分享一下你如何得到updateBool的价值吗?当值为'yes'时,该函数正确运行,因为它是函数中的默认逻辑。我相信问题在于你传递给updateBool的方式/价值是什么,即你没有引用你认为自己的单元格或你获得该值的方式是错误的。 (这是最好的猜测,我可以拿出有限的代码,我的假设也可能完全错误:)) –

回答

1

这似乎就好了工作:

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; 
    } 

    } 
+0

谢谢,但我不认为这会有所帮助。在原来的代码中,我使用“Number”将该值解析为数字类型。在这段代码中,Number函数没有完成那个?我不明白它将如何帮助返回值? –

0

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

callingCellRef.getValue()返回no和串号返回NaN,所以callingCellValue值为NaN

为了得到D4变化

callingCellValue = Number(callingCellRef.getValue()); 

callingCellValue = callingCellRef.getValue();