【问题标题】:Tranfer data from a sheet to an html form将数据从工作表传输到 html 表单
【发布时间】:2021-07-26 22:01:23
【问题描述】:

我正在研究如何使用 Google 脚本将数据从工作表传输到表单中的下拉列表

我已将数据收集在一个表格中,我认为解决此问题的最佳解决方案是通过 JS 脚本传输数据。 (如果您有更好的选择,请告诉我)

我在这个链接之后做了一些测试:Set Google Script variable in JavaScript

这是我的代码:

Main.gs

/**
 * Retrieves unique datas in a given column
 * @param column, the column where are the datas
 * @return uniqueDatas
 */
function searchUniqueDatas(column) {
  var range = sheet.getRange(5, column, sheetLastRow);
  var rangeData = range.getValues();
  var uniqueDatas = [];

  // Gather all the column datas
  for (var i=0; i < rangeData.length; i++) {
    uniqueDatas = uniqueDatas.concat(rangeData[i+1]);
  }
  return uniqueDatas = uniqueDatas.filter(onlyUnique);
}

var typeData = searchUniqueDatas(1); // The unique data are in a table. This works perfectly

AddCustomer.GS

// I tried to do as the link said
    function getData(data) {
      return data;
    }
    
    function doGet(){
      return HtmlService.createHtmlOutputFromFile('FormAddCsutomer');
    }

AddCustomerForm.html

  <label for="type">Type *</label><br/>
  <select multiple id="type" name="type" required>
    <!-- dropdown to fill -->
  </select><br/><br/>

希望我说清楚了。

Cooper 回答后编辑

我按照您的代码做了一些测试。不过还是不行。

您能帮我找出问题所在吗?

谷歌脚本

// getData(data) and doGet() have been deleted
function getSelectOptions()
{
  sortOptions();
  var options=[];
  for(var i=0; i < typeData.getValues().length; i++)
  {
    options.push(typeData.getValues()[i][0]);
  }
  return typeData.getValues();
}

HTML

      <label for="type">Type</label><br/>
      <select name="type" id="type">
        <option value="" selected></option>
      </select><br/><br/>


<script>
    $(function() {
        $('#txt1').val('');
        google.script.run
          .withSuccessHandler(updateSelect)
          .getSelectOptions();
      });

    function updateSelect(vA) {
      var select = document.getElementById("type");
      select.options.length = 0; 
      for(var i=0; i < vA.length; i++)
      {
        select.options[i] = new Option(vA[i],vA[i]);
      }
    }
    </script>

新编辑

@Cooper:按照您的表格示例,

.gs

function getUniqueCustomers () {
  return customerData.getValues(); // Return a 1-dimension table with the data
}

.html

<form id="filterForm" onSubmit="handleFormSubmitFilter(this)">

      <label for="customer">Customer</label><br/>
      <select name="customer" id="customer">
        <? var vs = getUniqueCustomers();
          for (var i=0; i < vs.length; i++) { ?>
          <option> <?= vs[i]  ?> </option>
        <?
         }
        ?>
      </select><br/><br/>
    ...
</form>

我尝试了 for 和 forEach。它一次显示“”而不是数据。

我想知道之间的代码是否被识别。

有什么想法吗?

8 月 6 日编辑,

仍在尝试解决此问题。

我尝试关注这个 tuto 视频,但我无法调整我的代码(我的工作表中只有一列,我希望每个选项的文本、值和 id 都是相同的数据)。 https://www.youtube.com/watch?v=pmQdrAIdfGM

这是我的代码。 感谢您的帮助。

// Return a simple array with all the sheet values. It works
function getUniqueCustomers () {
  return customerData.getValues();
}

HTML

      <label for="customer">Customer</label><br/>
      <select name="customer" id="customer" onChange="onSelectCustomer()">
      <script>loadCustomer();</script>
      </select><br/><br/>

<script>
function loadCustomer() {
  google.script.run.withSuccessHandler(function(ar) {
    var customerSelect = document.getById('customer');

    let option = document.createElement('option');
    option.value = "";
    option.text = "";
    customerSelect.appendChild(option);

    ar.forEach(function (item) {
      option.value = item[0];
      option.text = item[0];
      option.id = item[0];
      customerSelect.appendChild(option);
    });
  }).getList();
};

function onSelectCustomer() {
  var id = document.getElementById("customer").value;
  document.getElementById("carValue").innerHTML = id;
};
</script>

【问题讨论】:

  • typeData 和 sortOptions() 未定义。
  • getSelectOptions() 如果使用 JQuery,应该从 window.onload 或 $(function) 调用

标签: html forms google-apps-script google-sheets


【解决方案1】:

从 html 表单启动操作

你可以使用google.script.run.withSuccessHandler( data =&gt; {//display data here}).getData()

这是另一个将数据写入对话框的示例。

GS:

function getMySheetData() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const vs = sh.getDataRange().getValues();
  return vs;
}

function launchMyTableDialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createTemplateFromFile('ah1').evaluate().setWidth(800),'Title');
}

html:(文件名:ah1.html)

<!DOCTYPE html>
 <html>
   <head>
     <title>My Title</title>
   </head>
   <body>
     <div id="tabledata">
       <? var vs = getMySheetData(); ?>
       <table>
         <? vs.forEach((r,i)=>{ ?>
           <tr>
           <? r.forEach((c,j)=>{ ?>
             <? if(i == 0) { ?>
            <th style="padding:2px 5px;font-weight:bold;border:1px solid black;"><?= c ?> </th>           
           <? } else { ?>
             <td style="padding:2px 5px;border:1px solid black;"><?= vs[i][j] ?> </td>
           <? } ?>
         <?  }); ?>
           </tr>
         <? }); ?>
       </table>
     </div>

我的 Sheet1:

COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10
13 5 18 13 6 8 17 13 4 11
13 19 13 6 15 4 19 11 7 18
5 11 7 17 7 14 9 12 11 6
13 5 3 14 6 11 15 7 3 14
11 2 11 17 5 15 18 16 6 16
16 12 7 13 10 3 6 18 14 5
6 3 15 0 2 15 9 4 2 18
1 6 12 15 2 17 11 13 14 16
11 1 7 4 17 12 0 5 12 19
6 1 8 6 2 2 15 18 7 2
18 2 0 16 4 19 12 2 16 15
19 11 6 16 10 3 15 8 4 16
14 14 14 17 15 3 8 2 6 9
13 14 10 12 15 4 5 4 9 5
1 0 13 19 10 19 5 11 9 12
1 10 19 18 4 4 8 7 1 1
18 12 9 15 18 5 5 19 9 1
19 6 11 0 8 2 7 0 9 10
12 11 1 2 7 1 11 3 12 0
14 18 13 10 2 5 11 18 7 2

对话框:

【讨论】:

  • 感谢您的回答。你能解释一下这条线是如何工作的吗?如何将其作为选项插入到下拉列表中?
  • 嗨,我编辑了我的主题,因为我无法在评论部分发布答案。如果你没有看到它,我会 ping 你。谢谢:)
  • 您好,本主题之前的任何建议都忘记了
猜你喜欢
  • 2023-01-10
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2014-03-06
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
相关资源
最近更新 更多