如何使用 node.js {name} 更新 google 驱动器文档中的特定文本
How to update specific text in google drive docs with node.js {name}
我正在尝试使用 google 驱动器 API 更新 google 驱动器文档中的单个文本,例如 {name}。例如,如果我在文档中有变量 name = 'hello world',则文本 {name} 应替换为 'hello world'。感谢您的帮助。
async function replaceText() {
try {
const id = "144zvxc_xQUuEqKgS5BfjOKMQsfegFI2Vey";
drive.files.update(
{
fileId: id,
requests = [
{
replaceAllText: {
containsText: {
text: "{{firstname}}",
matchCase: true,
},
replaceText: 'hello world',
},
},
]
},
);
console.log(response);
} catch (error) {
console.log(error.message);
}
}
我认为您的目标和现状如下。
- 您想将 Google 文档中的
{{firstname}}
替换为 hello world
。
- 您想使用 googleapis 实现此目的 Node.js。
- 您已经能够使用 Docs API.
获取和放置 Google 文档的值
很遗憾,Drive API 中的更新方法中没有 replaceAllText
请求。这种情况请使用Google DocsAPI的batchUpdate方法。当你的脚本修改后,变成如下。
修改后的脚本:
这种情况下,请使用https://www.googleapis.com/auth/documents
的范围。
const docs = google.docs({ version: "v1", auth }); // Please use `auth` of your script.
const documentId = "###"; // Please set your Google Document ID.
const requests = [
{
replaceAllText: {
containsText: {
text: "{{firstname}}",
matchCase: true,
},
replaceText: "hello world",
},
},
];
docs.documents.batchUpdate(
{
auth,
documentId: documentId,
requestBody: {
requests,
},
},
(err, res) => {
if (err) {
console.log(e);
return;
}
console.log(res.data);
}
);
- 当您想将
{name}
替换为hello world
时,请将上面的脚本从text: "{{firstname}}",
修改为text: "{name}",
。
注:
- 在此修改中,假设您已经能够使用 Google 文档 API 获取和放置 Google 文档的值。请注意这一点。
参考文献:
我正在尝试使用 google 驱动器 API 更新 google 驱动器文档中的单个文本,例如 {name}。例如,如果我在文档中有变量 name = 'hello world',则文本 {name} 应替换为 'hello world'。感谢您的帮助。
async function replaceText() {
try {
const id = "144zvxc_xQUuEqKgS5BfjOKMQsfegFI2Vey";
drive.files.update(
{
fileId: id,
requests = [
{
replaceAllText: {
containsText: {
text: "{{firstname}}",
matchCase: true,
},
replaceText: 'hello world',
},
},
]
},
);
console.log(response);
} catch (error) {
console.log(error.message);
}
}
我认为您的目标和现状如下。
- 您想将 Google 文档中的
{{firstname}}
替换为hello world
。 - 您想使用 googleapis 实现此目的 Node.js。
- 您已经能够使用 Docs API. 获取和放置 Google 文档的值
很遗憾,Drive API 中的更新方法中没有 replaceAllText
请求。这种情况请使用Google DocsAPI的batchUpdate方法。当你的脚本修改后,变成如下。
修改后的脚本:
这种情况下,请使用https://www.googleapis.com/auth/documents
的范围。
const docs = google.docs({ version: "v1", auth }); // Please use `auth` of your script.
const documentId = "###"; // Please set your Google Document ID.
const requests = [
{
replaceAllText: {
containsText: {
text: "{{firstname}}",
matchCase: true,
},
replaceText: "hello world",
},
},
];
docs.documents.batchUpdate(
{
auth,
documentId: documentId,
requestBody: {
requests,
},
},
(err, res) => {
if (err) {
console.log(e);
return;
}
console.log(res.data);
}
);
- 当您想将
{name}
替换为hello world
时,请将上面的脚本从text: "{{firstname}}",
修改为text: "{name}",
。
注:
- 在此修改中,假设您已经能够使用 Google 文档 API 获取和放置 Google 文档的值。请注意这一点。