简体   繁体   English

在Google App脚本上将数组输入值传递给code.gs

[英]Passing array input values to code.gs on google app scripting

I have a form with input array and I want to initialized the input value by another Global array value and pass the value to code.gs . 我有一个带有输入数组的表单,我想用另一个全局数组值初始化输入值,并将该值传递给code.gs。 But I couldn't get the value I passed in code.gs. 但是我无法获得我在code.gs中传递的值。 Would you help me please? 你能帮我吗? Thank you! 谢谢!

Html part HTML部分

 <form id="downloadpdf">
      <input id="urlclass" type="text" name="urlname[]"  />
      <input type="submit" value="Download" />
    </form>

Javascript 使用Javascript

<script>   
      var urllink=[];                                      // Global variable a return from another function 

       $(document).ready(function() {

            function another(){
                var url=[];      // it is a dynamic value I get it from another function  eg. url[0]=”www.abc.com” ,url[1]=”www.abcd.com”, url[2]=”www.abcde.com”....
                  urllink.push(url);                                    // Assigning the global array value by the urls 
                }

           $("#downloadpdf").submit(function() { 
                $("#urlclass").val(urllink);                     // assigning the input value by the Global array value.                       
                google.script.run.withSuccessHandler(function(retsearch){ 

                }).downloadthefile(this);

                });

                });

 </script>

Code.gs Code.gs

function downloadthefile(urlpass){
Logger.log(urlpass.urlname);                                  // I couldn’t get the urls here  

}

This is a simple html file communicating with Google Apps Script contained in a Spreadsheet. 这是一个与电子表格中包含的Google Apps脚本通信的简单html文件。 I test it a lot as a dialog and I also run it as a web app. 我将其作为对话框进行了大量测试,并且还将其作为Web应用程序运行。 The html file and the Google Apps Script communicate with each other and I pass the contents of 4 textboxes back to the Google Script as an array. html文件和Google Apps脚本相互通信,我将4个文本框的内容作为数组传递回Google脚本。 Hope this helps. 希望这可以帮助。

The Code.gs file: Code.gs文件:

function doGet()
{
  var html = HtmlService.createHtmlOutputFromFile('index');
  return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)

}

function getData(a)
{
  var ts = Utilities.formatDate(new Date(), "GMT-6", "M/d/yyyy' 'HH:mm:ss");
  a.splice(0,0,ts);
  var ss=SpreadsheetApp.openById('SPREADSHEETID')
  ss.getSheetByName('Form Responses 1').appendRow(a);
  return true;
}

function getURL()
{
  var ss=SpreadsheetApp.openById('SPREADSHEETID');
  var sht=ss.getSheetByName('imgURLs');
  var rng=sht.getDataRange();
  var rngA=rng.getValues();
  var urlA=[];
  for(var i=1;i<rngA.length;i++)
  {
    urlA.push(rngA[i][0]);
  }
  return urlA;
}

The index.html file: index.html文件:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <div id="data">
    <br />Text 1<input name="t1" type="text" size="15" id="txt1" placeholder="Text 1" />
    <br />Text 2<input name="t2" type="text" size="15" id="txt2" placeholder="Text 2" />
    <br />Text 3<input name="t3" type="text" size="15" id="txt3" placeholder="Text 3" />
    <br />Text 4<input name="t4" type="text" size="15" id="txt4" placeholder="Text 4" />
    <br /><input type="radio" name="Type" value="Member" checked />Member
    <br /><input type="radio" name="Type" value="Guest" />Guest
    <br /><input type="radio" name="Type" value="Intruder" />Intruder
    <br /><input type="button" value="submit" id="btn1" />
    <br /><img id="img1" src="" alt="img1" width="300" />
  </div>
  <div id="resp" style="display:none;">
    <h1>Response</h1>
    <p>Your data has been received.</p>
  </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
      $(function() {
        $('#btn1').click(validate);
        $('#txt4').val('');
        $('#txt3').val('');
        $('#txt2').val('');
        $('#txt1').val('')
        google.script.run
          .withSuccessHandler(setURL)
          .getURL();
      });
      function setURL(url)
      {
        $('#img1').attr('src',url[0]);
      }
      function setResponse(a)
      {
        if(a)
        {
          $('#data').css('display','none');
          $('#resp').css('display','block');
        }
      }

      function validate()
      {
        var txt1 = document.getElementById('txt1').value || '';
        var txt2 = document.getElementById('txt2').value || '';
        var txt3 = document.getElementById('txt3').value || '';
        var txt4 = document.getElementById('txt4').value || '';
        var type = $('input[name="Type"]:checked').val();
        var a = [txt1,txt2,txt3,txt4,type];//This gets passed as an array;
        if(txt1 && txt2 && txt3 && txt4)
        {
          google.script.run
            .withSuccessHandler(setResponse)
            .getData(a);
            return true;
        }
        else
        {
          alert('All fields must be completed.');
        }
      }

      function loadTxt(from,to)
      {
          document.getElementById(to).value = document.getElementById(from).value;
      }

      function radioValue()
      {
        var radios = document.getElementsByName('genderS');
        for (var i = 0, length = radios.length; i < length; i++) 
        {
          if(radios[i].checked) 
          {
            return radios[i].value;
          }
        }
      }


     console.log('My Code');
   </script>
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Google App Script 中将变量从 Code.gs 传递到 html - Passing variable from Code.gs to html in Google App Script 将数组从Code.gs传递到Javascript Google App脚本 - Pass an Array from Code.gs to Javascript Google App Script 使用Google Apps脚本将对话框中输入字段的值返回到code.gs中的var - return value from input field in dialog box to var in code.gs using Google Apps Script 使用 html 对话框中输入字段的值使用 Google Apps 脚本在 code.gs 中进行 var - use value from input field in html dialog box to var in code.gs using Google Apps Script Google云端硬盘应用:如何获取HTML文件以读取code.gs中定义的变量? - Google drive app: How can i get my HTML file to read a varitable defined in code.gs? 从 code.gs Google App Script 内部获取变量值到 Javascript - Get Variable Value from inside code.gs Google App Script to Javascript 使用 Code.gs 中的 javascript 从数组值动态 html 表 - Dynamic html Table from an array values using javascript in Code.gs Google Apps脚本,将两个(变量)2个“变量”或“数组”从code.gs传递给index.html / Javascript - Google apps script, pass (two) 2 “variables” or “array” from code.gs to index.html / Javascript Google 脚本从 code.gs 返回到 index.html - Google script return from code.gs to index.html Code.gs中的Google Apps脚本返回对象 - Google Apps Script return object in Code.gs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM