如何使用谷歌;“文件”;javascript中的API BatchUpdate(需要代码)
我使用以下代码在谷歌文档中插入文本。但这不是工作。 谷歌也没有为batchupdate提供任何单一的JavaScript示例。有人知道那件事吗如何使用谷歌;“文件”;javascript中的API BatchUpdate(需要代码),javascript,google-docs-api,Javascript,Google Docs Api,我使用以下代码在谷歌文档中插入文本。但这不是工作。 谷歌也没有为batchupdate提供任何单一的JavaScript示例。有人知道那件事吗 function makeApiCall() { var updateObject = { documentId: 'My-Document-Id', resource: { requests: [{ "insertText": {
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
位置的
索引作为测试。在这种情况下,文本也可以插入到新文档中。因为我认为当使用
时,可能会发生错误25
- 但是我使用
- 我认为您的
脚本是为Node.js的googleapis编写的。所以我将其修改为Javascript。function(e,r){if(e){console.log(e);}else{console.log(r.data);}}
- 关于
,我想原因可能是这样的。因为在脚本中,不会返回响应它不起作用。
- 关于
- 这个修改后的脚本假设您已经通过Javascript使用了GoogleDocsAPI。当脚本的授权生效时,上述修改的脚本生效。如果出现与授权和Docs API相关的错误,请确认脚本以及是否启用了Docs API
- 为了测试这个脚本,我使用了
作为范围https://www.googleapis.com/auth/documents
如果我误解了你的问题,我很抱歉。请描述什么不起作用。您是否看到任何错误(例如使用F12工具)@Sameer Bayani感谢您的回复。我很高兴你的问题解决了。
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);
});
}