2017-08-11 106 views
0

我需要幫助來弄清楚如何在用於填充Google電子表格的html表單中更新選擇內容之後刷新輸入值。使用google.script.run重置html表單輸入

代碼:

function description(item){ 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheetByName('DATA'); 
    var cell = sheet.getRange('ITEM'); 

    cell.setFormula('=VLOOKUP(' + item + ';ITEMS!$C$2:$AE;29;FALSE)'); 

    return cell.getValue(); 

} 

HTML:

... 

<select id="SKU" onchange="update();"><?!= Drop_SKU();?></select> 

<input id="description" value="<?!= ss.getRange('ITEM').getDisplayValue();?>"> 

... 

<script> 

    window.update=function(){ 

     var ss = SpreadsheetApp.getActive(); 
     var parameter = document.getElementById('SKU').value 
     var newValue = google.script.run.description(parameter); 

     document.getElementById('description').value = newValue; 

    } 

其結果,描述功能完全運行,但會導致設定不定值的輸入 「原文」。

我錯過了什麼?

我會讚賞你的幫助。先謝謝你。

回答

0

google.script.run有兩片,以確定如何在服務器端代碼運行後做什麼: .withSuccessHandler(successFunction) .withFailureHandler(failedFunction)

如果服務器端功能,沒有運行錯誤,由withSuccessHandler定義的函數將運行,並且任何返回的值將傳遞給該函數。如果發生錯誤,則會執行withFailureHandler定義的函數。所以你的HTML腳本部分將如下所示:

<script> 

     window.update=function(){ 
     var ss = SpreadsheetApp.getActive(); 
     var parameter = document.getElementById('SKU').value 
     google.script.run.withSuccessHandler(showNewValue).withFailureHandler(functionFailed).description(parameter); 
    } 

    function showNewValue(newValue){ 
     document.getElementById('description').value = newValue; 
    } 

    function functionFailed(error){ 
     document.getElementById('description').value = 'Error returned: ' + error; 
    } 
</script> 

請注意,我沒有看過其餘代碼以查看是否還有其他問題。

+0

感謝您的幫助。但是,很抱歉,這段代碼根本沒有提供任何操作。什麼都沒發生。 「描述」都不在服務器端運行。 –

+0

不,我終於成功了!有一些打字錯誤。非常感謝!!! –

+0

對不起。如果可以的話,編輯我的帖子以修復輸入錯誤,以便其他人準確輸入。 –