如何在侧边栏和 <script> 中使用 Google Sheet 数据?

How can I use Google Sheet data in a sidebar and also within a <script>?

我目前正在使用以下教程在边栏中显示 google sheet 数据:https://yagisanatode.com/2018/02/12/how-to-get-something-from-google-sheets-and-display-it-in-the-sidebar-in-google-apps-script/#more-219

但是,我想在我的 html 文件中添加一个 "for" 循环来显示多个复选框(请参见下面的代码)。最后,我想将数字“16”更改为我的点差 sheet 中的 "rangeResult" 值。您能否提供有关如何执行此操作的建议?

Code.gs - 函数

function numEarlyRelease() {  
  var doc = SpreadsheetApp.openById("1OF6Y1CTU9dkIgd1P-nw-5f2lqHSS5cGZytndwzJhw-o");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var numValues = ss.getRange('R19').getValue();
  return numValues;
}

Page.html 文件

<!DOCTYPE html>
<html>
 <head>
   <base target="_top">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 </head>
  <body>
    <h3><script> document.write(new Date().toLocaleDateString()); </script></h3>

    <div id="rangeResult"></div>
    <script>
    function addRange(rangeStartEnd){ 
      $('#rangeResult').text(rangeStartEnd);
    };
    for (var i = 1; i <=16; i++) {
      document.write('<br><input type="checkbox" name="scores" id="i" value="i">'+ i);
    }

    google.script.run.withSuccessHandler(addRange).numEarlyRelease();
    </script> 

    <br><input type="button" value="Submit Check-In" onclick="google.script.host.close()" />
    <input type="button" value="Close" onclick="google.script.host.close()" />   
  </body>
</html>

感谢您的帮助!

函数调用-

google.script.run.withSuccessHandler(addRange).numEarlyRelease();

表示如果函数 numEarlyRelease() returns 一个值,在本例中为 numValues,它可作为函数 addRange() 的参数。 addRange() 有一个参数 rangeStartEnd,所以它的值是 numValues。你可以随心所欲地使用它。例如,如果它是一个数字 -

for (var i = 1; i <=rangeStartEnd; i++) {
  document.append('<br><input type="checkbox" name="scores" id="i" value="i">'+ i);
}

注意:请勿使用 document.write(),除非您想覆盖页面中已有的所有内容。

感谢您的建议!以下是我的解决方案。 (我更改了一些变量的名称以适合我的项目。)

Code.gs

function earlyRelease(e) {
  var doc = SpreadsheetApp.openById("1OF6Y1CTU9dkIgd1P-nw-5f2lqHSS5cGZytndwzJhw-o");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var numValues = ss.getRange('R19').getValue();
  var studentName = ss.getSheetValues(20, 18, numValues, 1);

  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setTitle('Early Release')
      .setWidth(300);
  SpreadsheetApp.getUi().showSidebar(html);  }

function earlyReleaseList() {
  var doc = SpreadsheetApp.openById("1OF6Y1CTU9dkIgd1P-nw-5f2lqHSS5cGZytndwzJhw-o");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var numValues = ss.getRange('R19').getValue();
  var studentNames = ss.getSheetValues(20, 18, numValues, 1);
  return studentNames; }

Page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  </head>
  <body>
    <script>
    function addStudents(studentList){ 
      $('#rangeResult').text(studentList);

      document.write(new Date().toLocaleDateString());

      var students = [];
      //The following loop was added to the function
      for (var i = 0; i < studentList.length; i++) {
        document.write('<br><input type="checkbox" name="students" id="i" value="i">'+ studentList[i]);
      }
      //Also added the buttons to the function with document.write rather than html tags outside of the function
      document.write('<br><input type="button" value="Submit Early Release" onclick="google.script.host.close()" />');
      document.write('<input type="button" value="Close" onclick="google.script.host.close()" />');
    };

    google.script.run.withSuccessHandler(addStudents).earlyReleaseList();
    </script> 
  </body>
</html>