如何 Google "Docs" API 在 javascript 中批量更新(需要代码)

How To Google "Docs" API BatchUpdate in javascript (Code Required)

我使用以下代码在 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);
          }
      });
  }

这个修改怎么样?

修改点:

  • 我认为您的请求正文是正确的。
    • 但是我用1作为locationindex作为测试。在这种情况下,文本也可以插入到新文档中。因为我觉得用25的时候可能会报错
  • 我认为您的 function (e, r) {if (e) {console.log(e);} else {console.log(r.data);}} 脚本适用于 Node.js 的 googleapis。所以我将其修改为Javascript。
    • 关于Its Not Work.,我觉得可能是这个原因。因为在你的脚本中,没有返回任何响应。

修改后的脚本:

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);
  });
}

注:

  • 此修改后的脚本假定您已经使用 Google 文档 API Javascript。当您的脚本授权有效时,上述修改后的脚本有效。如果出现授权和DocsAPI相关的错误,请确认脚本和DocsAPI是否启用。
  • 为了测试这个脚本,我使用https://www.googleapis.com/auth/documents作为范围。

参考:

如果我误解了你的问题,我深表歉意。