从作为变量传递的Spreadsheet生成HTML

时间:2017-05-21 15:52:04

标签: google-apps-script

是否可以将HTML作为网络应用提供,并在每次访问时传递特定的电子表格ID以查看电子表格特定数据?我有一组相同数据格式的电子表格,只有许多人拥有的不同数据值。我想要实现的是让他们选择以预定义的html格式查看他们的数据,只允许具有电子表格访问权限的用户查看数据。

1 个答案:

答案 0 :(得分:2)

需要将搜索字符串添加到Web App的已发布URL的末尾:

  

script.google.com/macros/s/FILE_ID_Here/exec?id=12345678

搜索字符串部分为:?id=12345678

doGet(e)函数可以使用对象的parameterparameters属性将搜索字符串作为对象。此示例显示如何从搜索字符串中获取id

var EXAMPLE_OF_GLOBAL_VARIABLE;//Put global variables in all CAPS

function doGet(e) {
  var searchStringAsObject,spreadsheetID,template;

  searchStringAsObject = e.parameters;//Get the search string that was appended to the URL of this published Web App as an object
  Logger.log('searchStringAsObject: ' + JSON.stringify(searchStringAsObject))

  spreadsheetID = searchStringAsObject.id;//For this to work- the search string must look like this:  urlHere?id=1234567
  //For example:
  //script.google.com/macros/s/FILE_ID_Here/exec?id=12345678

  Logger.log('spreadsheetID: ' + spreadsheetID)

  if (spreadsheetID) {
    EXAMPLE_OF_GLOBAL_VARIABLE = "The spreadsheet ID is: " + spreadsheetID;
  }

  template = HtmlService.createTemplateFromFile('Index');

  // Build and return HTML in IFRAME sandbox mode.
  return template.evaluate()
      .setTitle('Web App Window Title')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
};

的index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>
    This is a heading
    <br>
    <br>
    <?= EXAMPLE_OF_GLOBAL_VARIABLE || "No Data"?>
  </body>
</html>