【问题标题】:Writing Add-on manifest files编写附加清单文件
【发布时间】:2020-06-24 11:30:23
【问题描述】:

我使用 Packt 的《学习 Google Apps 脚本》一书中的示例在我的云端硬盘上创建了一个 GAS 脚本文件。我的问题是如何将其变成附加组件。不太确定我需要创建的清单文件(即使在阅读 https://developers.google.com/gmail/add-ons/how-tos/building 之后)。

function saveEmailAttachmentsToDrive(){

  // Create 'Gmail Attachments' folder if not exists.
  createFolder_('Gmail attachments');

// Get inbox threads starting from the latest one to 100.
  var threads = GmailApp.getInboxThreads(0, 100);

  var messages = GmailApp.getMessagesForThreads(threads);

  var folderID = PropertiesService.getUserProperties()
      .getProperty("FOLDER");

  var file, folder = DriveApp.getFolderById(folderID);

  for (var i = 0 ; i < messages.length; i++) {
    for (var j = 0; j < messages[i].length; j++) {
      if(!messages[i][j].isUnread()){

        var msgId = messages[i][j].getId();

        // Assign '' if MSG_ID is undefined.
        var oldMsgId = PropertiesService.getUserProperties()
            .getProperty('MSG_ID') || '';

        if(msgId > oldMsgId){
          var attachments = messages[i][j].getAttachments();

          for (var k = 0; k < attachments.length; k++) {
            PropertiesService.getUserProperties()
              .setProperty('MSG_ID', messages[i][j].getId());

            try {
              file = folder.createFile(attachments[k]);
              Utilities.sleep(1000);// Wait before next iteration.
            } catch (e) {
              Logger.log(e);
            }
          }

        }
        else return;

      }
    }
  }

};


function createFolder_(name) {
  var folder, folderID, found = false;

  /*
   * Returns collection of all user folders as an iterator.
   * That means it do not return all folder names at once, 
   * but you should get them one by one.
   *
   */
  var folders = DriveApp.getFolders();

  while (folders.hasNext()) {
    folder = folders.next();
    if (folder.getName() == name) {
      folderID = folder.getId();
      found = true;
      break;
    }
  };

  if (!found) {
    folder = DriveApp.createFolder(name);
    folderID = folder.getId();
  };

  PropertiesService.getUserProperties()
    .setProperty("FOLDER", folderID);

  return folderID;
}

我的问题是如何将其变成附加组件。不太确定我需要创建的清单文件(即使在阅读 https://developers.google.com/gmail/add-ons/how-tos/building 之后)。

【问题讨论】:

  • 您必须一次性支付 5 美元的费用才能发布附加组件。你这样做了吗?看起来您正在尝试创建一个 GMail 插件,并且在清单中为此配置了一个特殊的 gmail 属性。其他类型的附加组件不需要对清单文件进行任何操作。您需要查看的文档是:Link to Documentation 您应该可以只复制该 JSON,然后修改设置。
  • 试图复制并过去你链接到我的清单,但得到'意外字符('-'(代码 46)):期待双引号在 [Source: { .. .'
  • 我添加了一个答案。

标签: google-apps-script gmail-addons gsuite-addons


【解决方案1】:

gmail 部分的清单文件的 JSON 是:

"gmail": {
  "name": "My Gmail Add-on",
  "logoUrl": "https://www.example.com/hosted/images/2x/my-icon.png",
  "primaryColor": "#4285F4",
  "secondaryColor": "#00BCD4",
  "authorizationCheckFunction": "get3PAuthorizationUrls",
  "contextualTriggers": [{
    "unconditional": {},
    "onTriggerFunction": "buildAddOn"
  }]

那一定是除了已经存在的东西之外。它的清单文件非常基本,如下所示:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER"
}

你要让它看起来像下面这样:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "gmail": {
    "name": "My Gmail Add-on",
    "logoUrl": "https://www.example.com/hosted/images/2x/my-icon.png",
    "primaryColor": "#4285F4",
    "secondaryColor": "#00BCD4",
    "authorizationCheckFunction": "get3PAuthorizationUrls",
    "contextualTriggers": [{
      "unconditional": {},
      "onTriggerFunction": "buildAddOn"
    }]
  },
  "exceptionLogging": "STACKDRIVER"
}

使用您的时区和设置。请注意,没有列出任何依赖项,但请保留该部分。

【讨论】:

  • 感谢 Sandy 提供清单文件。那个有效。那么现在我是选择 Deploy as web add-on 还是从 manifest 中选择 Deploy?
  • 您必须使用“从清单部署”在快速入门文档Link to Quickstart for Gmail Add-on中有更多信息
  • 再次感谢。当我从清单转到 Deploy 时,我现在得到了两个部署。我刚刚在头部部署中进行了“获取 ID”并成功安装了它。但是当我运行它时,我得到:附加组件出错。运行时错误。找不到脚本函数:buildAddOn
  • 查看设置"onTriggerFunction": "buildAddOn",它试图运行一个名为buildAddOn的函数。 onTriggerFunction 是在满足某些条件时导致加载项运行的原因。在您当前拥有的清单中,条件是unconditional,因此无论电子邮件中的内容如何,​​插件都会尝试运行函数buildAddOn。发生的情况是,用户打开一封电子邮件,插件会查找contextualTriggers 设置中定义的条件。你有一个saveEmailAttachmentsToDrive 函数。将buildAddOn 替换为saveEmailAttachmentsToDrive
  • 再次感谢。确实做出了改变。但是现在当我运行插件时,我得到:插件出错。运行时错误。 Apps 脚本返回的值的类型不能被插件平台使用。还要确保在返回之前调用任何构建器的构建。值:Null、未定义、空或空白
【解决方案2】:

manifest.json 由 Apps 脚本引擎自动生成。默认情况下它是隐藏的,但您可以通过切换查看>清单文件来查看它以查看结构。

当您准备好测试插件时,您可以转到发布 > 部署为 Web 插件。如果您从云编辑器上传,则无需在网络商店列表中添加单独的清单文件。

详细的开发文档covers deployment methods

【讨论】:

  • 我选择发布 --> 部署为 Web 插件而不是发布 --> 从清单部署?
  • 您的插件是独立脚本吗? (不绑定到 Doc、Sheet、Slide 或 Form)。如果是,请选择“部署为 Web 插件”。对于 Gmail 插件,选择“从清单部署”。
  • 这是一个独立的脚本。我希望它可用于某些邮件数据库。当我选择 Deploy as web add-on 时,有一个下拉选项让我选择 Docs、Sheets、Forms 和 Slides。我真的不想要这些。所以我有点困惑。提前感谢您的帮助。
猜你喜欢
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 2016-10-11
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
相关资源
最近更新 更多