2017-07-28 56 views
0

我試圖在Google表格中實現自定義HTML菜單。通過函數getValuesForRngName()從下拉列表中填入來自命名範圍的值。在列表中選擇值後,腳本應該再次從表單中檢索範圍,確定價格並將價格填充到文本框servicePrice中。但是,問題是,這行代碼「values = google.script.run.getValuesForRngName('ServiceList');」不檢索任何內容。google.script.run在自定義菜單中的使用

您能否建議如何使它工作?

<div> 
 
    <form> 
 
    <table> 
 
     <tr> 
 
     <td>Select Service:</td> 
 
     <td><select name="servicesMD" id="servicesMD" onChange="retrieveServicePrice(this);"></select></td> 
 
     </tr> 
 
     <tr> 
 
     <td><br/><br/> 
 

 
      <script type="text/javascript"> 
 
      function onSuccess(values) { 
 
       var option, dropDown; 
 
       for (i = 1; i < values.length; i++) { 
 
       dropDown = document.getElementById("servicesMD"); 
 
       option = document.createElement("option"); 
 
       dropDown.options.add(option); 
 
       option.value = values[i][0]; // service ID 
 
       option.text = values[i][1]; // service Name 
 
       } 
 
      } 
 

 
      function populate() { 
 
       google.script.run.withSuccessHandler(onSuccess).getValuesForRngName('ServiceList'); 
 
      } 
 
      </script> 
 
      Service Price: <input id="servicePrice" name="servicePrice" type="text" /><br/><br/> 
 
     </tr> 
 
    </table> 
 
    </form> 
 
</div> 
 

 
<script> 
 
    window.addEventListener('load', populate()); 
 

 
    function retrieveServicePrice(element) { 
 
    var selectionIndex; 
 
    var values; 
 
    values = google.script.run.getValuesForRngName('ServiceList'); 
 
    selectionIndex = document.forms[0].action = element.options[element.selectedIndex].getAttribute("value"); 
 
    document.getElementById("servicePrice").value = values[selectionIndex][3]; // should retrieve price of service 
 
    } 
 
</script>

GAS代碼如下::

function onOpen() { 
 
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp. 
 
     .createMenu('Custom Menu') 
 
     .addItem('Show custom menu', 'openServiceSalesForm') 
 
     .addToUi(); 
 
} 
 
function openServiceSalesForm() { 
 
    var html = HtmlService.createHtmlOutputFromFile('Page') 
 
     .setWidth(600) 
 
     .setHeight(400); 
 
     SpreadsheetApp.getUi().showModalDialog(html, 'Service sales form'); 
 
} 
 

 
// RangeList 
 
function getValuesForRngName(rngName) { 
 
    var rngValues = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(rngName).getValues(); 
 
    return rngValues; 
 
}

+0

rngValues是一個二維陣列。如果只有一個,那麼它將被rngValues [0] [0]引用; – Cooper

回答

0

假設,在範圍ServiceList列是在

HTML代碼下面提供以下訂單Service ID | Service Name | Price

<script> 
    window.addEventListener('load', populate()); 
    var serviceID; 

    function retrieveServicePrice(element) { 
     serviceID = element; 
     google.script.run.withSuccessHandler(injectValuesIntoHTML).getValuesForRngName('ServiceList'); 
    } 
    window.injectValuesIntoHTML = function(values) { 
     for (var i = 0; i < values.length; i++) { 
      if (serviceID == values[i][0]) { 
       document.getElementById("servicePrice").value = values[i][2]; // should retrieve price of service 
      } 
     } 
    } 
</script> 

HTML:

<select name="servicesMD" id="servicesMD" onChange="retrieveServicePrice(this.value);"> 
+1

謝謝利茲!這個版本終於完美:) –