首页 > 解决方案 > 在 Google Workspace 插件中获取工作表和幻灯片编辑器事件对象时出现问题

问题描述

我正在制作一个 Google Workspace 插件,我想在其中获取工作表对象,例如

"sheets": {
            "addonHasFileScopePermission": true,
            "id":"A_24Q3CDA23112312ED52",
            "title":"How to get started with Sheets"
          },

但是当我尝试使用文档时,我没有得到工作表对象,我只能得到文档对象。这是文档和工作表的示例事件对象,如下所示:

//In case of docs, I am getting docs object with details title, id etc. For docs

{ clientPlatform: 'web',
  docs: 
   { title: 'How to get started with Docs',
     id: 'A_24Q3CDA23112312ED52sdsff',
     addonHasFileScopePermission: true },
  hostApp: 'docs',
  userCountry: 'GB',
  commonEventObject: 
   { userLocale: 'en-GB',
     platform: 'WEB',
     hostApp: 'DOCS',
     timeZone: { id: 'Asia/Kolkata', offset: 19800000 } },
  userLocale: 'en',
  userTimezone: { offSet: '19800000', id: 'Asia/Kolkata' } }
      
//But in Case of Sheets and Slides, I am not getting  objects sheets or slides with details title, id etc. For Sheets  
  

{ clientPlatform: 'web',
  userLocale: 'en',
  hostApp: 'sheets',
  userCountry: 'GB',
  userTimezone: { offSet: '19800000', id: 'Asia/Kolkata' },
  commonEventObject: 
   { hostApp: 'SHEETS',
     platform: 'WEB',
     userLocale: 'en-GB',
     timeZone: { id: 'Asia/Kolkata', offset: 19800000 } } }

这是我的附加清单文件

{
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
                      "https://www.googleapis.com/auth/script.external_request",
                      "https://www.googleapis.com/auth/script.locale",
                      "https://www.googleapis.com/auth/userinfo.email",
                      "https://www.googleapis.com/auth/drive.file",
                      "https://www.googleapis.com/auth/drive.addons.metadata.readonly"
                 ],
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Add_on",
      "logoUrl": "{my logo url}",
      "layoutProperties": {
        "primaryColor": "#2772ed"
      },
      "useLocaleFromApp": true,
      "homepageTrigger": {
        "runFunction": "onDriveHomePageOpen"
      },
    },
    "drive": {
      "homepageTrigger": {
        "runFunction": "onDriveHomePageOpen",
        "enabled": true
      },
      "onItemsSelectedTrigger": {
        "runFunction": "onDriveItemsSelected"
      }
    },
    "docs": {
      "homepageTrigger": {
        "runFunction": "onDocsHomepage"
      },
      "onFileScopeGrantedTrigger": {
        "runFunction": "onFileScopeGrantedEditors"
      }
    },
    "sheets": {
      "homepageTrigger": {
        "runFunction": "onSheetsHomepage"
      },
      "onFileScopeGrantedTrigger": {
        "runFunction": "onFileScopeGrantedEditors"
      }
    },
    "slides": {
      "homepageTrigger": {
        "runFunction": "onSlidesHomepage"
      },
      "onFileScopeGrantedTrigger": {
        "runFunction": "onFileScopeGrantedEditors"
      }
    }
  }
}

示例代码如下:

function onDocsHomepage(e) {
  console.info(e);
  return createAddOnView(e);
}

function onSheetsHomepage(e) {
  console.info(e);
  return createAddOnView(e);
}

function onSlidesHomepage(e) {
  console.info(e);
  return createAddOnView(e);
}

function createAddOnView(e) {
  var docsEventObject;
  if(e['hostApp'] == 'docs'){
   docsEventObject = e['docs'];
  }
  if(e['hostApp'] == 'sheets'){
    docsEventObject = e['sheets'];
  }
  if(e['hostApp'] == 'slides'){
    docsEventObject = e['slides'];
  }
  var builder =  CardService.newCardBuilder();
  var cardSection = CardService.newCardSection();
  if (docsEventObject!=null && docsEventObject['addonHasFileScopePermission']!=null) {
    cardSection.setHeader(docsEventObject['title']);
    // This add-on uses the recommended, limited-permission `drive.file`
    // scope to get granular per-file access permissions.
    // See: https://developers.google.com/drive/api/v2/about-auth
    // If the add-on has access permission, read and display its quota.
    cardSection.addWidget(
      CardService.newTextParagraph().setText("File Id: " + docsEventObject['id']));
  } else {
    // If the add-on does not have access permission, add a button that
    // allows the user to provide that permission on a per-file basis.
    cardSection.addWidget(
      CardService.newTextParagraph()
      .setText( "The add-on needs permission to access this file." ));

    var buttonAction = CardService.newAction()
      .setFunctionName("onRequestFileScopeButtonClicked");

    var button = CardService.newTextButton()
    .setText("Request permission")
      .setOnClickAction(buttonAction);
    cardSection.addWidget(button);
  }
  return builder.addSection(cardSection).build();
}

function onRequestFileScopeButtonClicked(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();
}

function onFileScopeGrantedEditors(e){
  console.info("after granting item");
  console.info(e);
  return createAddOnView(e);
}

在上面的代码中,在附加组件中对文件进行光栅访问后,我只能获取文档对象而无法获取工作表和幻灯片对象。

根据文档,这里是一个示例编辑器事件对象(用于工作表):https ://developers.google.com/workspace/add-ons/editors/gsao/building-editor-interfaces#event_objects 和请求文件访问的示例代码对于当前文档:https ://developers.google.com/workspace/add-ons/editors/gsao/editor-actions#request_file_access_for_current_document

问题是什么?为什么我没有得到工作表和幻灯片的对象?仅给出的示例适用于文档,不适用于工作表和幻灯片。

标签: google-apps-scriptgoogle-workspacegoogle-workspace-add-ons

解决方案


这似乎是一个错误!

我冒昧地在 Google 的问题跟踪器上为您报告了这一点,并详细说明了行为:

您可以点击页面左上角问题编号旁边的 ☆,让 Google 知道更多人正在遇到此问题,因此更有可能更快地被看到。


推荐阅读