【问题标题】:How to create a spreadsheet with an associated script?如何创建带有关联脚本的电子表格?
【发布时间】:2019-09-21 22:10:25
【问题描述】:

我想以编程方式创建一批包含不同数据的电子表格,但所有这些电子表格都包含一个与自定义后端函数关联的按钮。

例如,每个电子表格都应该有一个按钮,当按下该按钮时,可以将数据导出到另一个工作表。

这样的事情可能吗?

我的一个想法可能是创建一个包含按钮和相关应用程序脚本的模板,然后制作该电子表格的副本并用自定义数据填充它。

【问题讨论】:

  • 可以复制另一个包含其脚本的电子表格。
  • 如果您试图避免让用户在每次使用新电子表格时都需要授权脚本,那么我认为除了使用附加组件之外没有任何方法可以避免这种情况.
  • 为自己省去很多麻烦,从正确的开始......使用附加模型进行分发

标签: google-apps-script


【解决方案1】:

Apps-Script API 允许您以编程方式创建 Apps 脚本项目,并可选择将它们绑定到 Google 表格

  • 将您的项目转换为 Cloud Platform 项目和 enable 此项目的 Apps Script API
  • 在清单文件中为您的项目提供必要的范围
  • 通过 Urlfetch 请求将 Apps 脚本 API 合并到 Apps 脚本中
  • Create a new Apps Script ProjectMethod: projects.create 指定 parentId
  • Add contents 给项目Method: projects.updateContent。您可以将内容存储在一个变量中,从而将相同的内容添加到您的所有 Apps 脚本项目中。

示例: JSON文件

{
  "timeZone": "America/New_York",
  "oauthScopes": [
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.projects",
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER"
}

.gs 文件

function createSpreadsheetwithScript() {

  var ss=SpreadsheetApp.create('mySpreadsheet');
  var id=ss.getId();
  var token = ScriptApp.getOAuthToken();
  var url = "https://script.googleapis.com/v1/projects";
  var payload = {
    "title": "myAutoCreatedScript",
    "parentId": id
  }
  var options = {
    "method" : "POST",
    "muteHttpExceptions": true,
    "headers": {
       'Authorization': 'Bearer ' +  token
     },
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  }
  var response = UrlFetchApp.fetch(url,options);
  var scriptId=JSON.parse(response).scriptId;
  var url2="https://script.googleapis.com/v1/projects/"+scriptId+"/content";
 //test content 
  var source="function myFunction() {\n var x=1;\n}";
  var JSONsource="{\"timeZone\":\"America/New_York\",\"exceptionLogging\":\"STACKDRIVER\"}";  
   var payload2 = {
  "files": [
    {
      "name": "this is the gs. file",
      "type": "SERVER_JS",
      "source": source
    },
    {
      "name": "appsscript",
      "type": "JSON",
      "source": JSONsource,
   "updateTime":"2018-03-04T19:49:08.871Z",
    "functionSet":{
      "values":[{"name":"myFunction"}]}
    }
  ]
}

var options2 = {
  "headers": {
     'Authorization': 'Bearer ' + token,
   }, 
  "contentType": "application/vnd.google-apps.script+json",
  "method" : "PUT", 
  "payload": JSON.stringify(payload2)
  }
  var response2 = UrlFetchApp.fetch(url2,options2); 
}

https://script.google.com/home/usersettings 下使用它之前,请确保enable the Apps-script API 并且您的upadteContent 请求包含一个清单文件

More samples

【讨论】:

    【解决方案2】:

    编写自定义函数:

    在 Google 表格中创建或打开电子表格。

    选择菜单项工具 > 脚本编辑器。如果出现欢迎屏幕,请单击左侧的空白项目以开始一个新项目。

    删除脚本编辑器中的任何代码......

    选择菜单项文件 > 保存.... 全部完成!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      相关资源
      最近更新 更多