2

我使用 Packt 的“Learning Google Apps Script”一书中的示例在我的 Drive 上创建了一个 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之后)。

4

2 回答 2

3

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"
}

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

于 2018-02-14T21:41:52.467 回答
1

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

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

开发文档详细介绍了部署方法

于 2018-02-13T19:38:32.177 回答