【问题标题】:Transfer a HTML table (user input) to google sheets将 HTML 表格(用户输入)传输到 Google 表格
【发布时间】:2019-09-03 03:46:11
【问题描述】:

我想使用 google 的应用脚本将一些用户输入数据从 html 页面传输到我的 google 表格。我可以为单个值执行此操作,但无法找到为表格执行此操作的好方法。该表将由用户动态创建(固定列数和动态行数)。

下面的代码显示了单个值的示例,并且缺少整个表的解决方案。

一个想法是遍历表并使用表数据创建一个数组数组,然后将其解析到后端脚本,然后将其写入工作表,但也许有更聪明的方法可以做到这一点。

HTML 前端:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

  <!-- Get the user name -->
  <label>Name:</label><input type="text" id="username">

  <!-- Get the table data -->
  <table class="table table-bordered" id="tbl_posts">
        <thead>
          <tr>
            <th>#</th>
            <th>column 1</th>
            <th>column 2</th>
            <th>column 3</th>
            <th>column 4</th>
          </tr>
        </thead>
        <tbody id="tbl_posts_body">
          <tr id="rec-1">
            <td><span class="sn">1</span>.</td>
            <td>11</td>
            <td>12</td>
            <td>13</td>
            <td>14</td>
          </tr>
        </tbody>
        <tbody id="tbl_posts_body">
          <tr id="rec-2">
            <td><span class="sn">1</span>.</td>
            <td>21</td>
            <td>22</td>
            <td>23</td>
            <td>24</td>
          </tr>
        </tbody>
      </table>

    <button id="sendbutton"> Send </button>

    <script>

    document.getElementById("sendbutton").addEventListener("click",parse_to_backend);

    function parse_to_backend(){

      // transfer the username
      var name = document.getElementById("username").value;
      google.script.run.TransferUsername(name);

      // transfer the table data 

      // HERE I NEED SOME CODE

    };

   </script>

  </body>
</html>

JS 后端:

function doGet() {

  return HtmlService.createHtmlOutputFromFile("FrontEndScript");

}


function TransferUsername(name){

  var URL = "https://docs.google.com/spreadsheets/...";
  var Spreadsheet = SpreadsheetApp.openByUrl(URL);
  var Worksheet = Spreadsheet.getSheetByName("Data");
  Worksheet.appendRow([name,]);

}

function TransferTabledata(tabeldata){

  var URL = "https://docs.google.com/spreadsheets/...";
  var Spreadsheet = SpreadsheetApp.openByUrl(URL);
  var Worksheet = Spreadsheet.getSheetByName("Data");

  // Here i need some code

}

【问题讨论】:

标签: html google-apps-script web-applications google-sheets client-server


【解决方案1】:

您可以使用 Sheets API 直接解析和粘贴 HTML。

FrontEnd.html 片段:

let table = document.getElementById('tbl_posts').outerHTML;
google.script.run.pasteHtml(table);

Code.gs 片段:

function pasteHtml(table) {
  var ss = SpreadsheetApp.getActive();
  var req = {
    requests: [
      {
        pasteData: {
          html: true,
          data: table,
          coordinate: {
            sheetId: ss.getSheets()[1].getSheetId(), //second sheet!A1
            rowIndex: 0,
            columnIndex: 0,
          },
        },
      },
    ],
  };
  Sheets.Spreadsheets.batchUpdate(req, ss.getId());
}

阅读:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2012-05-02
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    相关资源
    最近更新 更多