【问题标题】:DocumentList API and GAS. How to marry them?DocumentList API 和 GAS。如何嫁给他们?
【发布时间】:2012-05-14 13:43:04
【问题描述】:

使用这个 API 怎么样?

developers.google.com/google-apps/documents-list

它似乎能够访问修订历史提要。

但是这些示例是在 .NET 中的。如何在 Google Apps 脚本中应用/使用此 API?任何人都知道如何并且可以阐明一些问题吗?也许是一个简短的示例代码?

谢谢。

【问题讨论】:

    标签: google-docs-api google-apps-script google-spreadsheet-api


    【解决方案1】:

    您需要查看 DocLists API 的协议。您可以将此协议与 URLFetch 和 Google oAuth 一起使用 这是一个简单的示例,它以 json 格式返回修订历史记录

    //Get revison history
    //resource_id is the id of the doc
    function getRevisionHistory(resource_id){
      var scope = 'https://docs.google.com/feeds/';
      var fetchArgs = googleOAuth_('docs', scope);
      fetchArgs.method = 'GET';
      var url = scope + 'default/private/full/'+resource_id+'/revisions?v=3&alt=json';
      var urlFetch = UrlFetchApp.fetch(url, fetchArgs);
      var jsonFeed = Utilities.jsonParse(urlFetch.getContentText()).feed.entry;
      return jsonFeed
    }
    
    //Google oAuth
    //Used by getRevisionHistory
    function googleOAuth_(name,scope) {
      var oAuthConfig = UrlFetchApp.addOAuthService(name);
      oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
      oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
      oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
      oAuthConfig.setConsumerKey("anonymous");
      oAuthConfig.setConsumerSecret("anonymous");
      return {oAuthServiceName:name, oAuthUseToken:"always"};
    }
    

    要更多地使用 DocsList API,您可以在我的 Google 网站上查看更多示例 https://sites.google.com/site/appsscripttutorial/urlfetch-and-oauth

    【讨论】:

    • 感谢瓦卡尔的回复。我有几个问题: Resource_ID 是文档/电子表格键吗?顺便说一下,我想连接到电子表格。如何调用该函数?像这样? getRevisionHistory(spreadsheet_key)
    • 是的,Resource_ID 是 Document/spreadsheet 的键。要调用该函数,只需在代码中调用 getRevisionHistory('spreadsheet/document ID Here')。确保上述功能已作为单独的项目复制到同一项目中。
    • 感谢瓦卡尔。我现在就试试。然后我需要学习解析JSON。还不知道该怎么做。谢谢。
    【解决方案2】:

    查看:https://sites.google.com/site/scriptsexamples/custom-methods/driveservice 只需将源代码复制到您的文件中即可。

    我将在接下来的几周内添加缺少的方法。 修订肯定在我的清单上。

    您将能够使用这些库,例如: DOCS_LIST_API.GdocToFormat(docID,格式)

    不需要设置OAuth,它会被内置。

    【讨论】:

    • 嗨詹姆斯。听起来不错。但我不知道如何使用它。有什么入门技巧吗?
    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2022-12-30
    • 2018-11-30
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2014-07-13
    相关资源
    最近更新 更多