2017-02-13 106 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();