2017-07-22 8 views
1

Googleスプレッドシートにフォームを作成し、入力値を受け取り、それらを現在のシートに追加するスクリプトを作成しました。変数名を持つ入力値にアクセスしようとするまで、これは完全にうまく動作します。 が私は番号が入力されフォーム名に変数名でアクセスする方法は?

私は現在の入力を取得しようとしているに焦点を当てています私はが名前で作成されたの「価格」フィールドに入力「vPrice」+(I + 1)以前は「変動数」numVarです。 varItemAdd(IN

Iは、(vPrice1、vPrice2等)個別の値にアクセスすることができ、それらは正しい値を生成します。また、numVarの値にアクセスすることもできますが、vPrice変数をスプレッドシートの各値を生成するように段階的に調整しようとすると、 'は定義されていません。'となります。

スクリプト:

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]; 
+1

ありがとう:

var vPriceInput = form.vPrice; 

に!変数にドット演算子を使用できないことに気付かなかった。 –

関連する問題