如何在javascript中使用Google“Docs”API BatchUpdate(需要代码)

问题描述 投票:1回答:1

我在Google文档中使用以下代码插入文字。但它不起作用。 Google也没有在JavaScript中为batchupdate提供任何单个示例。有人知道那件事吗?

  function makeApiCall() {
      var updateObject = {
          documentId: 'My-Document-Id',
          resource: {
              requests: [{
                  "insertText": {
                      "text": "Sameer Bayani",
                      "location": {
                          "index": 25,
                      },
                  },
              }],
          },
      };
      gapi.client.docs.documents.batchUpdate(updateObject, function (e, r) {
          if (e) {
              console.log(e);
          } else {
              console.log(r.data);
          }
      });
  }
javascript google-docs-api
1个回答
0
投票

这个修改怎么样?

Modification points:

  • 我认为您的请求正文是正确的。 但我使用1作为indexlocation作为测试。在这种情况下,文本也可以插入到新文档中。因为我认为使用25时可能会发生错误。
  • 我认为你的function (e, r) {if (e) {console.log(e);} else {console.log(r.data);}}脚本是针对Node.js的googleapis。所以我把它修改为Javascript。 关于Its Not Work.,我认为原因可能是这个。因为在您的脚本中,不会返回任何响应。

Modified script:

function makeApiCall() {
  var updateObject = {
    documentId: 'My-Document-Id',
    resource: {
      requests: [{
        insertText: {
          text: "Sameer Bayani",
          location: {
            index: 1, // Modified
          },
        },
      }],
    },
  };
  gapi.client.docs.documents.batchUpdate(updateObject)
  .then(function(res) { // Modified
    console.log(res);
  },function(err) {
    console.error(err);
  });
}

Note:

  • 此修改后的脚本假设您已经使用了Javascript的Google Docs API。当您的脚本授权有效时,上面修改的脚本可以正常工作。如果发生与授权和文档API相关的错误,请确认脚本以及是否启用了Docs API。
  • 为了测试这个脚本,我使用https://www.googleapis.com/auth/documents作为范围。

Reference:

如果我误解了你的问题,我道歉。

© www.soinside.com 2019 - 2024. All rights reserved.