使用脚本将 HTML table 数据发送回 Google 工作表

Send HTML table data back to Google Sheets using Scripts

我正在使用 this website

中的示例

我正在尝试将 HTML table 中的值添加到活动电子表格的 a1 单元格中。我有一种感觉,甚至没有调用函数 runsies(values)

问题是,我在日志中什么也没有。 理想的解决方案将是用户在单独的单元格中选择的结果。例如:A1、A2等中的橙色1、蓝色2等

我做错了什么?

Code.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>

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

更新

基于@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([值["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);

};