簡體   English   中英

使用腳本將 HTML 表數據發送回 Google 表格

[英]Send HTML table data back to Google Sheets using Scripts

我正在使用這個網站的一個例子

我正在嘗試將 HTML 表中的值添加到活動電子表格的 a1 單元格中。 我有一種感覺,甚至沒有調用 function runsies(values)

問題是,我在日志中什么也沒有。 理想的解決方案將是用戶選擇分離單元的結果。 例如:A1、A2 等中的橙色 1、藍色 2 等

我做錯了什么?

代碼.js

//--GLOBALS--

var ui = SpreadsheetApp.getUi();


function onOpen(e){
  // Create menu options
  ui.createAddonMenu()
    .addSubMenu(ui.createMenu("Admin")
      .addItem("Test","test"))
    .addToUi();
};

function test(){ 
  //Call the HTML file and set the width and height
  var html = HtmlService.createHtmlOutputFromFile("testUI")
    .setWidth(450)
    .setHeight(300);

  //Display the dialog
  var dialog = ui.showModalDialog(html, "Select the relevant module and unit");


};
//--GLOBALS--

var ui = SpreadsheetApp.getUi();


function onOpen(e){
  // Create menu options
  ui.createAddonMenu()
    .addSubMenu(ui.createMenu("Admin")
      .addItem("Test","test"))
    .addToUi();
};

function test(){ 
  //Call the HTML file and set the width and height
  var html = HtmlService.createHtmlOutputFromFile("testUI")
    .setWidth(450)
    .setHeight(300);

  //Display the dialog
  var dialog = ui.showModalDialog(html, "Select the relevant module and unit");

};

function runsies(values){
  //Display the values submitted from the dialog box in the Logger and in the A1 cell of active sheet. 
  var sheet = SpreadsheetApp.getActiveSheet(); 
  sheet.getRange(1,1).setValue([values])
  Logger.log(values);
};

HTML

<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  </head>
  <body>
    <div>

        <table>
          <col width="60">
          <col width="50">
          <col width="50">
          <col width="50">
          <col width="50">
          <col width="50">
          <col width="50">
          <col width="50">
          <col width="50">
          <col width="50">
          <tr>
            <th></th><th><strong>Unit:</strong></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th>
          </tr>
          <tr>
            <th><strong>Module</strong></th>
            <th><strong>n/a</strong></th>
            <th><strong>1</strong></th>
            <th><strong>2</strong></th>
            <th><strong>3</strong></th>
            <th><strong>4</strong></th>
            <th><strong>5</strong></th>
            <th><strong>6</strong></th>
            <th><strong>7</strong></th>
            <th><strong>8</strong></th>
          </tr>
          <tr>
            <td>Orange </td>
            <td><input type="radio" name="orange" value="na" checked></td>
            <td><input type="radio" name="orange" value="1"></td>
            <td><input type="radio" name="orange" value="2"></td>
            <td><input type="radio" name="orange" value="3"></td>
            <td><input type="radio" name="orange" value="4"></td>
            <td><input type="radio" name="orange" value="5"></td>
            <td><input type="radio" name="orange" value="6"></td>
            <td><input type="radio" name="orange" value="7"></td>
            <td><input type="radio" name="orange" value="8"></td>
          </tr>
          <tr>
            <td>Blue </td>
            <td><input type="radio" name="blue" value="na" checked></td>
            <td><input type="radio" name="blue" value="1"></td>
            <td><input type="radio" name="blue" value="2"></td>
            <td><input type="radio" name="blue" value="3"></td>
            <td><input type="radio" name="blue" value="4"></td>
            <td><input type="radio" name="blue" value="5"></td>
            <td><input type="radio" name="blue" value="6"></td>
            <td><input type="radio" name="blue" value="7"></td>
            <td><input type="radio" name="blue" value="8"></td>
          </tr>
          <tr>
            <td>Green </td>
            <td><input type="radio" name="green" value="na" checked></td>
            <td><input type="radio" name="green" value="1"></td>
            <td><input type="radio" name="green" value="2"></td>
            <td><input type="radio" name="green" value="3"></td>
            <td><input type="radio" name="green" value="4"></td>
            <td><input type="radio" name="green" value="5"></td>
            <td><input type="radio" name="green" value="6"></td>
            <td><input type="radio" name="green" value="7"></td>
            <td><input type="radio" name="green" value="8"></td>
          </tr>
          <tr>
            <td>Purple </td>
            <td><input type="radio" name="purple" value="na" checked></td>
            <td><input type="radio" name="purple" value="1"></td>
            <td><input type="radio" name="purple" value="2"></td>
            <td><input type="radio" name="purple" value="3"></td>
            <td><input type="radio" name="purple" value="4"></td>
            <td><input type="radio" name="purple" value="5"></td>
            <td><input type="radio" name="purple" value="6"></td>
            <td><input type="radio" name="purple" value="7"></td>
            <td><input type="radio" name="purple" value="8"></td> 
          </tr>
        </table>
        <input type="submit" value="Submit" class="action" onclick="form_data()" >
        <input type="button" value="Close" onclick="google.script.host.close()" />


    </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    </script>
    <script>
      function form_data(){
        var values = [{
          "orange":$("input[name=orange]:checked").val(),
          "blue":$("input[name=blue]:checked").val(),
          "green":$("input[name=green]:checked").val(),
          "purple":$("input[name=purple]:checked").val()
        }];
        google.script.run.withSuccessHandler(closeIt()).runsies(values);
        closeIt()
      };
      function closeIt(){
        google.script.host.close()
      };

    </script>
  </body>
</html>

Htm 表是這樣的

==================================================== =====

更新

基於@Cooper 的回答。 我改變了這個google.script.run.withSuccessHandler(closeIt()).runsies(values); 到這個google.script.run.withSuccessHandler(closeIt).runsies(values); 這有幫助,現在我在 A1 中得到{orange=1, purple=1, blue=1, green=1}

使用該代碼sheet.getRange(1,1).setValue([values])

如何在不同的單元格中分離這些數據?

例如:在單元格 A1 中得到 1 而不是{orange=1, purple=1, blue=1, green=1}

我想,我有這個代碼的字典類型數據(在 python 中稱為這種方式)。

var values = {
          "orange":$("input[name=orange]:checked").val(),
          "blue":$("input[name=blue]:checked").val(),
          "green":$("input[name=green]:checked").val(),
          "purple":$("input[name=purple]:checked").val()
        };

如何僅獲得橙色值?


字典代碼有錯誤:

var values = {
          "orange":$("input[name=orange]:checked").val(),
          "blue":$("input[name=blue]:checked").val(),
          "green":$("input[name=green]:checked").val(),
          "purple":$("input[name=purple]:checked").val()
        };

我在找那個 sheet.getRange(1,1).setValue([values["orange"]])

嘗試這個:

function runsies(values){
  var sheet = SpreadsheetApp.getActiveSheet(); 
  sheet.getRange(1,1,4,1).setValues([[values.orange],[values.blue],[values.purple],[values.green]]);
  Logger.log(values);

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM