【问题标题】:How to read Google sheet cells and display it to an HTML form如何读取 Google 表格单元格并将其显示为 HTML 表单
【发布时间】:2019-07-28 03:14:41
【问题描述】:

这将被部署为一个网络应用程序,我需要从 Google 工作表文档中获取一系列值并将其显示在我的 HTML 表单上。

我需要从 temp 变量中获取单元格值并将其传递给文本表单。到目前为止,我有应用脚本的代码,但我不知道如何将它传递给 HTML 表单。

Code.gs //Google App 脚本的代码

function getValues() 
{

  var app = SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet();

  var temp = activeSheet.getRange("A1:C1").getValues();

  Logger.log(temp); //I need to get the values from the temp variable and pass it to the text forms.
}

Index.html //HTML 索引页面

<form>
  A1:<br>
  <input type="text" name="a1" value="">
  <br>

  B1:<br>
  <input type="text" name="b1" value="">
  <br>

  C1:<br>
  <input type="text" name="b1" value="">

  <br><br>
  <input type="submit" value="Fetch">
</form>

【问题讨论】:

标签: javascript google-apps-script google-sheets-api


【解决方案1】:

你可以这样做

GS 文件

function getValues() 
{

  var app = SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet();
  var temp = activeSheet.getRange("A1:C1").getValues();       
  return temp;
}

function doGet() {
  return HtmlService
      .createTemplateFromFile('Index')
      .evaluate()
}

HTML 文件

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>  
  <? var temp = getValues(); ?>  
  <form>
  A1:<br>
  <input type="text" name="a1" value=" <?= temp[0][0] ?> ">
  <br>
  B1:<br>
  <input type="text" name="b1" value=" <?= temp[0][1] ?> ">
  <br>
  C1:<br>
  <input type="text" name="b1" value=" <?= temp[0][2] ?> ">
  <br><br>
  <input type="submit" value="Fetch">
  </form> 
  </body>
  </html>

打印脚本&lt;? ?&gt; 允许您评估 HTML 代码中的 Apps 脚本变量,您甚至可以直接访问打印脚本中的电子表格内容。请查找here 更多关于打印脚本的信息。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多