如何将数据从 google appscript 发送到 html 页面

How to send data from google appscript to an html page

我有一个具有多项功能的 google 应用脚本。我想在侧边栏 html 页面 上显示一些数据。这是我测试过的。

function getColumnData(sheetName){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
    var rangeData = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()).getValues();
    var firstRow = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    var firstRowMapped = firstRow.reduce(function(a, b) {return a.concat(b)}); // to convert 2d to 1d
    Logger.log(firstRowMapped);
    return firstRowMapped;
} // returns the first Row t html form for population

在 html 方面,我想获取此函数返回的任何内容。

function getRow() {
        var sheetName = document.getElementById("sheetname").value.toString();
        var firstRow = getColumnData(sheetName);
        var checkBox = document.getElementById("checkboxes");
        var firstRow = google.script.run.getColumnData(sheetName)
        for (var columns in firstRow){
        checkBox.innerHTML += `<label>
        <input type="checkbox" value="${firstRow[columns]}"/>
        <span>${firstRow[columns]}</span>
      </label><br>`
      }
      }

根据我的理解,这应该是可行的。但遗憾的是它没有得到任何数据。 有什么想法吗?

这对我有用。您必须传递一个函数才能将数据接收到 withSuccessHandler()

google.script.run.withSuccessHandler("a function that will receive the data")."the function that gets the data"
// in my case
      function getRow() {
        var sheetName = document.getElementById("sheetname").value.toString();

        google.script.run
          .withSuccessHandler(populateChecks)
          .getColumnData(sheetName);
      }
      function populateChecks(firstRow) {
        var checkBox = document.getElementById("checkboxes");
        for (var columns in firstRow) {
          checkBox.innerHTML += `<label>
        <input type="checkbox" value="${firstRow[columns]}"/>
        <span>${firstRow[columns]}</span>
      </label><br>`;
        }
      }