2017-07-22 60 views
1

我製作了一個腳本,可以在Google電子表格中生成一個表單,將其輸入值附加到當前表單中。所有這些工作都很好,直到我嘗試訪問具有變量名稱的輸入值。如何使用變量名稱訪問表單值?

我目前專注於試圖讓輸入進入其中與名字創建的「價格」字段「vPrice」 +(I + 1),其中數進入先前在「變體數量」numVar

varItemAdd()我可以單獨訪問值(vPrice1,vPrice2等)以及它們所產生的正確的值。我也可以訪問numVar的值,但是當我嘗試增量調整vPrice變量以生成電子表格上的每個值時,它會顯示爲'undefined'。

腳本:

function varItemAdd(form) { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 

    var number = form.numVar; 
    var attribNumber = form.numAttr; 

    sheet.appendRow([form.manufacturer, number, attribNumber]); 

    for (i=0;i<number;i++) {  
     var vPrice = "vPrice" + (i + 1); 
     var vPriceInput = form.vPrice; 
     sheet.appendRow([vPriceInput, number, attribNumber]); 
     } 
    return true; 
    } 

HTML

<body> 
    <form> 
     <!-- Select Number of Attributes to appear --> 
     <h2 class="title">Number of Attributes:</h2> 
     <input class="input-box" type="number" min="1" max="5" id="numAttr" name="numAttr" value="1"><br/> 

     <!-- Select Number of Variations to appear --> 
     <h2 class="title">Number of Variations:</h2> 
     <input class="input-box" type="number" id="numVar" name="numVar" value="1"><br/> 

     <h3 class="buttons" id="submit" onclick="addFields()">ADD</h3> 
     <div id="attBoxes"></div> 
     <div id="varBoxes"></div> 
     <br> 
     <input class="buttons" id="submit" type="button" value="SUBMIT" 
    onclick="google.script.run 
     //.withSuccessHandler(google.script.host.close) 
     .varItemAdd(this.parentNode)" /> 
     <input class="buttons" id="reset" type="reset" value="RESET"> 

    </form> 
</body> 

<script type='text/javascript'> 
    function addFields(){ 
     // Get number of variation inputs to create 
     var number = document.getElementById("numVar").value; 

     // Get number of attribute inputs to create 
     var attribNumber = document.getElementById("numAttr").value; 

     // Get container <div>s where dynamic content will be placed 
     var varBoxes = document.getElementById("varBoxes"); 
     var attBoxes = document.getElementById("attBoxes"); 

     // Clear previous contents of the container 
     while (varBoxes.hasChildNodes()) { 
      varBoxes.removeChild(varBoxes.lastChild); 
     } 
     while (attBoxes.hasChildNodes()) { 
      attBoxes.removeChild(attBoxes.lastChild); 
     } 
     attBoxes.appendChild(document.createTextNode("Attribute Name(s)")); 

     // For each attribute append an input box inside each variation 
     for (k=0;k<attribNumber;k++){    
      var attTitle = attBoxes.appendChild(document.createElement("h2")); 
      var attInput = attBoxes.appendChild(document.createElement("input")); 

      attTitle.textContent = "Attribute " + (k + 1); 

      attInput.type = "text"; 
      attInput.name = "v-att" + (k + 1); 
      attBoxes.appendChild(document.createElement("br")); 

      }; 
     attBoxes.appendChild(document.createElement("br")); 

     // For each variation create inputs 
     for (i=0;i<number;i++){ 
      varBoxes.appendChild(document.createTextNode("Variation " + (i+1))); 

      // Set variables 
      var skuTitle = varBoxes.appendChild(document.createElement("h2")); 
      var skuInput = document.createElement("input"); 
      var priceTitle = varBoxes.appendChild(document.createElement("h2")); 
      var priceInput = document.createElement("input"); 
      var attributes = varBoxes.appendChild(document.createElement("div")); 
      attributes.id = "varAttribs"; 
      var varAttribs = document.getElementById("varAttribs"); 

      // Set element values 
      skuTitle.textContent = "SKU"; 

      skuInput.type = "text"; 
      skuInput.name = "vSku"; 

      priceTitle.textContent = "Price"; 

      priceInput.type = "number"; 
      priceInput.id = "vPrice" + (i + 1); 
      priceInput.name = "vPrice" + (i + 1); 


      // Call elements 
      varBoxes.appendChild(skuTitle); 
      varBoxes.appendChild(skuInput); 
      varBoxes.appendChild(document.createElement("br")); 
      varBoxes.appendChild(priceTitle); 
      varBoxes.appendChild(priceInput); 
      varBoxes.appendChild(document.createElement("br")); 

      for (j=0;j<attribNumber;j++){ 
       var aValueTitle = varAttribs.appendChild(document.createElement("h2")); 
       var aValueInput = document.createElement("input"); 

       aValueTitle.textContent = "Attribute " + (j + 1) + " Value"; 
       aValueTitle.className = "title"; 

       aValueInput.type = "text"; 
       aValueInput.className = "input-box"; 
       aValueInput.name = "a-value-" + (j + 1); 

       varBoxes.appendChild(aValueTitle); 
       varBoxes.appendChild(aValueInput); 
       varBoxes.appendChild(document.createElement("br")); 
       }; 


      varBoxes.appendChild(document.createElement("br")); 
      varBoxes.appendChild(document.createElement("br")); 
     } 

    } 
</script> 

回答

2

只需更換腳本下面的線,那麼你應該能夠訪問每個價格元素的值。

var vPriceInput = form.vPrice; 

var vPriceInput = form[vPrice]; 
+1

這非常謝謝!沒有意識到你不能在變量上使用點運算符。 –