在Google Apps脚本上使用具有自定义功能的触发器

有营养的

我的脚本有问题,这个问题得到了极大的回答基本上,自定义函数无法调用需要授权的服务。但是,据我了解,如果我使用简单的触发器(例如onEdit),它可能会起作用。

我检查了上一个问题中建议的文档,但是未能成功将其应用于我的代码,您可以在下面看到:

function FileName (id) {
  var ss = DriveApp.getFileById(id);
  return ss.getName();
}

如何修改代码以使用简单的触发器?

这是一个复制该问题的样本表

Tanaike

我相信您的目标如下。

  • 您希望将的功能FileName用作Google Spreadsheet的自定义功能。
    • 当文件ID放入“ B”列时,您要自动检索文件名。
    • 您要将文件名放在“ C”列中。

问题和解决方法:

不幸的是,当使用自定义函数时,在当前规范中,除需要授权的几种方法(例如,其中一种是UrlFetchApp。)外,大多数方法无法使用。这样,DriveApp.getFileById(id)您的脚本中不能与自定义功能一起使用。但是有一种解决方法。在自定义功能上,可以使用UrlFetchApp。在这个答案中,我想建议使用带有UrlFetchApp的Web应用作为授权包装。这样,可以使用Web Apps完成授权。因此,您的函数可以通过自定义函数运行。

用法:

1.准备脚本。

请复制以下脚本并将其粘贴到脚本编辑器中并保存。

const key = "samplekey"; // This is a key for using Web Apps. You can freely modify this.

// This is your function.
function FileName_(id) {
  var ss = DriveApp.getFileById(id);
  return ss.getName();
}

// Web Apps using as the wrapper for authorizing.
function doGet(e) {
  let res = "";
  if (e.parameter.key === key) {
    try {
      res = FileName_(e.parameter.id);
    } catch (err) {
      res = `Error: ${err.message}`;
    }
  } else {
    res = "Key error.";
  }
  return ContentService.createTextOutput(JSON.stringify({value: res}));
}

function Filename(id) {
  const webAppsUrl = "https://script.google.com/macros/s/###/exec"; // Please set the URL of Web Apps after you set the Web Apps.

  const res = UrlFetchApp.fetch(`${webAppsUrl}?id=${id}&key=${key}`);
  if (res.getResponseCode() != 200) throw new Error(res.getContentText());
  return JSON.parse(res.getContentText()).value;
}

2.部署Web应用程序。

  1. 在脚本编辑器上,通过“发布”->“部署为Web应用程序”打开一个对话框。
  2. “将应用程序执行为:”中选择“我
    • 这样,脚本将作为所有者运行。
  3. “谁有权访问该应用程序:”选择“任何人,甚至匿名
    • 在这种情况下,不需要访问令牌即可向Web Apps请求。但是在此示例脚本中,使用了用于请求Web Apps的密钥。
  4. 单击“部署”按钮作为新的“项目版本”。
  5. 自动打开“需要授权”对话框。
    1. 点击“查看权限”。
    2. 选择自己的帐户。
    3. 在“此应用未验证”中,单击“高级”。
    4. 单击“转到###项目名称###(不安全)”
    5. 点击“允许”按钮。
  6. 点击“确定”。
  7. 复制Web Apps的URL。就像https://script.google.com/macros/s/###/exec
    • 修改Google Apps脚本后,请重新部署为新版本。这样,修改后的脚本将反映到Web Apps。请注意这一点。
  8. 请将URL设置https://script.google.com/macros/s/###/exec为上述脚本的url。并请重新部署Web Apps。这样,最新的脚本就会反映到Web应用程序中。所以请注意这一点。

3.测试此替代方法。

当文件ID放在单元格“ A1”中时,请=filename(A1)作为自定义功能放在单元格中。这样,脚本将运行并返回响应值。

注意:

  • 上面的示例脚本是用于测试脚本的简单示例脚本。因此,当您想使用各种方法时,这篇文章可能会有用。

  • 请在启用V8时使用此脚本。

  • 作为其他方法,我认为将文件ID手动放入“ B”列时,可以使用可安装的OnEdit触发器。示例脚本如下。请设置工作表名称。并且请将触发器安装到的功能installedOnEditRef这样,当文件ID放在的列“ B”时sheetName,文件ID放在“ C”列。

      function installedOnEdit(e) {
        const sheetName = "Sheet1";
        const range = e.range;
        const sheet = range.getSheet();
        if (!(sheet.getSheetName() == sheetName && range.getColumn() == 2 && range.getRow() > 1)) return;
        const value = range.getValue();
        let res = "";
        try {
          res = DriveApp.getFileById(value).getName();
        } catch(e) {
          res = e.message;
        }
        range.offset(0, 1).setValue(res);
      }
    

参考文献:

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Google Apps脚本上设置自定义频率触发器

来自分类Dev

具有HTTP触发器的自定义Domain-Google Cloud功能

来自分类Dev

在Google Apps脚本中将UrlFetchApp与触发器配合使用是否有限制

来自分类Dev

在Google Apps脚本中调试自定义功能

来自分类Dev

具有给定范围和参数的自定义函数-Google Apps脚本

来自分类Dev

如何使用Google Apps脚本制作自定义验证功能,例如requireTextMatchesPattern(pattern)函数?

来自分类Dev

在使用具有剪切和粘贴功能的Apps脚本时,如何处理Google表单中的新数据?

来自分类Dev

使用Google脚本对Google表格中的数据进行排序(具有自定义排序顺序)

来自分类Dev

在Google脚本的Google脚本内的特定工作表上使用onedit()触发器

来自分类Dev

在Google Apps脚本中将UrlFetchApp与触发器一起使用是否有限制

来自分类Dev

Google表格:具有动态更改的自定义功能

来自分类Dev

Google Tag Manager - Python - 创建自定义事件触发器

来自分类Dev

我的帐户中所有Google Apps脚本触发器的摘要

来自分类Dev

表单提交触发器上的Google App脚本-延迟

来自分类Dev

Google Apps脚本:onEdit在SpreadsheetApp.openById()上中断-需要在可安装的onEdit()触发器上输入

来自分类Dev

Google Apps脚本:仅针对新条目发送电子邮件触发器(不使用Google表单)

来自分类Dev

针对时间驱动的触发器的Google Apps脚本定义的时间范围不起作用

来自分类Dev

Google Apps脚本自定义样式菜单

来自分类Dev

在 Google Apps 脚本中是否有检测电子表格名称更改的触发器?

来自分类Dev

使用onFormSubmit触发器给定范围时如何写入单元格?(Google Apps脚本)

来自分类Dev

无法与自定义功能Google App脚本比较时间

来自分类Dev

使用Google Apps脚本提交自定义表单的时间戳

来自分类Dev

如何防止广告拦截器杀死使用google.script.host.close()的Google Apps脚本自定义对话框中的链接

来自分类Dev

Google脚本具有两个可进行编辑的触发器。我可以删除“简单”的吗?

来自分类Dev

具有自定义参数的样式触发器

来自分类Dev

具有通用功能的Google Cloud Dataflow自定义键

来自分类Dev

Google Apps脚本(自定义电子表格功能)示例中的“ input.map”是什么?

来自分类Dev

Google Apps脚本触发器每月的第一个星期四

来自分类Dev

如何在Google Apps脚本中以编程方式为打开事件创建触发器

Related 相关文章

  1. 1

    如何在Google Apps脚本上设置自定义频率触发器

  2. 2

    具有HTTP触发器的自定义Domain-Google Cloud功能

  3. 3

    在Google Apps脚本中将UrlFetchApp与触发器配合使用是否有限制

  4. 4

    在Google Apps脚本中调试自定义功能

  5. 5

    具有给定范围和参数的自定义函数-Google Apps脚本

  6. 6

    如何使用Google Apps脚本制作自定义验证功能,例如requireTextMatchesPattern(pattern)函数?

  7. 7

    在使用具有剪切和粘贴功能的Apps脚本时,如何处理Google表单中的新数据?

  8. 8

    使用Google脚本对Google表格中的数据进行排序(具有自定义排序顺序)

  9. 9

    在Google脚本的Google脚本内的特定工作表上使用onedit()触发器

  10. 10

    在Google Apps脚本中将UrlFetchApp与触发器一起使用是否有限制

  11. 11

    Google表格:具有动态更改的自定义功能

  12. 12

    Google Tag Manager - Python - 创建自定义事件触发器

  13. 13

    我的帐户中所有Google Apps脚本触发器的摘要

  14. 14

    表单提交触发器上的Google App脚本-延迟

  15. 15

    Google Apps脚本:onEdit在SpreadsheetApp.openById()上中断-需要在可安装的onEdit()触发器上输入

  16. 16

    Google Apps脚本:仅针对新条目发送电子邮件触发器(不使用Google表单)

  17. 17

    针对时间驱动的触发器的Google Apps脚本定义的时间范围不起作用

  18. 18

    Google Apps脚本自定义样式菜单

  19. 19

    在 Google Apps 脚本中是否有检测电子表格名称更改的触发器?

  20. 20

    使用onFormSubmit触发器给定范围时如何写入单元格?(Google Apps脚本)

  21. 21

    无法与自定义功能Google App脚本比较时间

  22. 22

    使用Google Apps脚本提交自定义表单的时间戳

  23. 23

    如何防止广告拦截器杀死使用google.script.host.close()的Google Apps脚本自定义对话框中的链接

  24. 24

    Google脚本具有两个可进行编辑的触发器。我可以删除“简单”的吗?

  25. 25

    具有自定义参数的样式触发器

  26. 26

    具有通用功能的Google Cloud Dataflow自定义键

  27. 27

    Google Apps脚本(自定义电子表格功能)示例中的“ input.map”是什么?

  28. 28

    Google Apps脚本触发器每月的第一个星期四

  29. 29

    如何在Google Apps脚本中以编程方式为打开事件创建触发器

热门标签

归档