应用程序脚本如何检测是否使用Doc,Sheet或其他类型的文档调用onOpen方法? [英] How can an App Script detect if the onOpen method is called with a Doc, Sheet, or other type of document?

查看:221
本文介绍了应用程序脚本如何检测是否使用Doc,Sheet或其他类型的文档调用onOpen方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在onOpen方法中,如何确定文档类型?

In an onOpen method, how can the document type be determined?

通过快速入门:Google文档附件建议使用以下代码:

From the Quickstart: Add-on for Google Docs the following code is suggested:

function onOpen(e) {
  DocumentApp.getUi().createAddonMenu()
      .addItem('Start', 'showSidebar')
      .addToUi(); 
}

但是,当打开Goog​​le表格时,脚本会引发异常:

However, when a Google Sheet is opened the script throws an exception:

Exception: Cannot call DocumentApp.getUi() from this context. at onOpen(Code:9:15)

首先应该进行测试,以检测正在打开的文档类型上下文,从而允许脚本选择是否以及如何添加菜单项.怎么做? onOpen 的参考表明e.source将是不同的类型,但是type of e.source仅是object.

There should be a test, first, detecting the document type context that is being opened, allowing the script to select if and how it will add menu items. How to do that? The reference for onOpen indicates e.source will be a different type, but type of e.source is only object.

欲望类似于:

function onOpen(e) {
  if (/* answer to this question: test if onOpen called for Doc only */) {
    DocumentApp.getUi().createAddonMenu()
        .addItem('Start', 'showSidebar')
        .addToUi(); 
  }
}

推荐答案

  • 您要在打开Goog​​le文档时检测Google文档的mimeType.然后,您要检索doc的对象.
  • 您想使用Google Apps脚本实现这一目标.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several answers.

    在此示例脚本中,当Google文档打开时,它将检索容器绑定脚本的活动文档.作为示例情况,当将此脚本用于Spreadsheet的容器​​绑定脚本时,DocumentApp.getActiveDocument()SlidesApp.getActivePresentation()FormApp.getActiveForm()返回null.并且只有SpreadsheetApp.getActiveSpreadsheet()返回该对象.这种方法使用这种情况.

    In this sample script, it retrieves the active document of the container-bound script when the Google Docs is opened. As the sample situation, when this script is used for the container-bound script of Spreadsheet, DocumentApp.getActiveDocument(), SlidesApp.getActivePresentation() and FormApp.getActiveForm() return null. And only SpreadsheetApp.getActiveSpreadsheet() returns the object. This method uses this situation.

    示例脚本如下.

    function onOpen() {
      var docObject = DocumentApp.getActiveDocument() ? DocumentApp :
          SpreadsheetApp.getActiveSpreadsheet() ? SpreadsheetApp :
          SlidesApp.getActivePresentation() ? SlidesApp :
          FormApp.getActiveForm() ? FormApp : null;
    
      // When this is used for your script, it becomes as follows.
      docObject.getUi().createAddonMenu()
            .addItem('Start', 'showSidebar')
            .addToUi();
    }
    

    • 例如,将上述脚本放入Google文档时,docObject成为DocumentApp的对象. docObject.getUi().createAddonMenu().addItem('Start', 'showSidebar').addToUi()适用于Google文档.
      • For example, when above script is put to Google Document, docObject becomes the object of DocumentApp. And docObject.getUi().createAddonMenu().addItem('Start', 'showSidebar').addToUi() works for the Google Document.
        • 在此脚本中,使用了以下4个作用域.
          • https://www.googleapis.com/auth/documents
          • https://www.googleapis.com/auth/forms
          • https://www.googleapis.com/auth/presentations
          • https://www.googleapis.com/auth/spreadsheets
          • In this script, the following 4 scopes are used.
            • https://www.googleapis.com/auth/documents
            • https://www.googleapis.com/auth/forms
            • https://www.googleapis.com/auth/presentations
            • https://www.googleapis.com/auth/spreadsheets
            • Class DocumentApp
            • Class SpreadsheetApp
            • Class SlidesApp
            • Class FormApp

            如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

            If I misunderstood your question and this was not the direction you want, I apologize.

            起初,我很抱歉误解了您的目标.从您更新的问题中,我可以理解如下.

            At first, I apologize I misunderstood your goal. From your updated question, I could understand as follows.

            • 您希望在容器绑定脚本的父级仅为Google Document时运行该脚本.

            如果我的理解是正确的,那么当使用我的回答方法时,如何进行以下修改?

            If my understanding is correct, when the method of my answer is used, how about the following modification?

            if (/* answer to this question: test if onOpen called for Doc only */) {
            

            收件人:

            if (DocumentApp.getActiveDocument()) {
            

            • 在这种情况下,当将其用于Google文档(除Google Document之外)时,DocumentApp.getActiveDocument()返回null.这样一来,除了Google文档外,if语句中的脚本不会针对Google Docs运行.
              • In this case, when this is used to Google Docs except for Google Document, DocumentApp.getActiveDocument() returns null. By this, the script in the if statement is not run for Google Docs except for Google Document.
              • 这篇关于应用程序脚本如何检测是否使用Doc,Sheet或其他类型的文档调用onOpen方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆