使用 Node 创建一个带有内置对象的 Google 文档
Create a Google Document with a built-in object using Node
我看过下面的 post 关于如何使用 Node.js 创建 Google 文档实例的文章。
Create a Google Document with Google Drive API and Node.js
但我还想传递一个对象,这样当创建 google 文档时,它就会将该对象存储在其环境中。有没有办法通过以下参数之一传递对象来做到这一点?
DRIVE.files.create({
resource: {
name: fileName,
mimeType: fileType
},
media: {
mimeType: fileType,
body: fileContent
}
}
任何帮助将不胜感激
- 您想通过将 Google Apps 脚本作为容器绑定脚本来创建新的 Google 文档。
- 作为示例情况,您想要包含
var obj = { “foo”:”bar”}
的脚本。
- 您想使用带有 Node.js 的 googleapis 来实现此目的。
如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。
在这个回答中,为了实现这一点,我在Google Apps Script API.
中使用了projects.create和projects.updateContent的方法
流量:
本次回答的流程如下
- 新建 Google 文档。
- 创建新的 GAS 项目作为已创建 Google 文档的容器绑定脚本。
- 将脚本放到创建的GAS项目中。
准备:
在你运行脚本之前,请准备如下。
- 请在 API 控制台启用 Google Apps 脚本 API。
- 根据你的问题,我认为你已经在 API 控制台启用了 Drive API。
- 请设置
https://www.googleapis.com/auth/drive
和https://www.googleapis.com/auth/script.projects
的范围。
- 请删除包含访问令牌和刷新令牌的凭据文件。因为这种方式用于将新范围反映到访问令牌。当你运行删除凭证文件后的脚本,授权过程是运行。所以请检索代码并使用代码检索访问令牌和刷新令牌。
- 请准备一个示例文本文件作为
sample.txt
的文件名。因为你的脚本中使用了 media
。
示例脚本:
const drive = google.drive({ version: "v3", auth });
const script = google.script({ version: "v1", auth });
drive.files.create(
{
requestBody: {
name: "sampleDocument",
mimeType: "application/vnd.google-apps.document"
},
media: {
mimeType: "text/plain",
body: fs.createReadStream("./sample.txt")
}
},
(err, res) => {
if (err) {
console.error(err);
return;
}
script.projects.create(
{
requestBody: {
title: "sampleGASProject",
parentId: res.data.id
}
},
(err, res) => {
if (err) {
console.log(err);
return;
}
script.projects.updateContent(
{
scriptId: res.data.scriptId,
auth,
resource: {
files: [
{
name: "Code",
type: "SERVER_JS",
source: 'var obj = {"foo":"bar"}\n'
},
{
name: "appsscript",
type: "JSON",
source:
'{"timeZone":"America/New_York","exceptionLogging":"STACKDRIVER"}'
}
]
}
},
(err, res) => {
if (err) {
console.log(err);
return;
}
console.log("Done.");
}
);
}
);
}
);
- 当您 运行 这个示例脚本时,新的 Google 文档被创建为
sampleDocument
的文件名,新的 GAS 项目被创建为 sampleGASProject
的项目名称.
- 脚本完成后,您可以在根文件夹中看到新的Google 文档。打开Google文档,打开脚本编辑器,可以看到
var obj = {"foo":"bar"}
. 的脚本
注:
- 如果您使用的是服务帐号,很遗憾,GAS 项目无法使用服务帐号进行管理。请注意这一点。所以请使用 OAuth2.
参考文献:
在我的环境中,我可以确认示例脚本有效。但如果这在您的环境中不起作用,我深表歉意。届时请检查API是否启用and/or其他环境。如果我误解了你的问题,这不是你想要的方向,我深表歉意。
我看过下面的 post 关于如何使用 Node.js 创建 Google 文档实例的文章。
Create a Google Document with Google Drive API and Node.js
但我还想传递一个对象,这样当创建 google 文档时,它就会将该对象存储在其环境中。有没有办法通过以下参数之一传递对象来做到这一点?
DRIVE.files.create({
resource: {
name: fileName,
mimeType: fileType
},
media: {
mimeType: fileType,
body: fileContent
}
}
任何帮助将不胜感激
- 您想通过将 Google Apps 脚本作为容器绑定脚本来创建新的 Google 文档。
- 作为示例情况,您想要包含
var obj = { “foo”:”bar”}
的脚本。
- 作为示例情况,您想要包含
- 您想使用带有 Node.js 的 googleapis 来实现此目的。
如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。
在这个回答中,为了实现这一点,我在Google Apps Script API.
中使用了projects.create和projects.updateContent的方法流量:
本次回答的流程如下
- 新建 Google 文档。
- 创建新的 GAS 项目作为已创建 Google 文档的容器绑定脚本。
- 将脚本放到创建的GAS项目中。
准备:
在你运行脚本之前,请准备如下。
- 请在 API 控制台启用 Google Apps 脚本 API。
- 根据你的问题,我认为你已经在 API 控制台启用了 Drive API。
- 请设置
https://www.googleapis.com/auth/drive
和https://www.googleapis.com/auth/script.projects
的范围。 - 请删除包含访问令牌和刷新令牌的凭据文件。因为这种方式用于将新范围反映到访问令牌。当你运行删除凭证文件后的脚本,授权过程是运行。所以请检索代码并使用代码检索访问令牌和刷新令牌。
- 请准备一个示例文本文件作为
sample.txt
的文件名。因为你的脚本中使用了media
。
示例脚本:
const drive = google.drive({ version: "v3", auth });
const script = google.script({ version: "v1", auth });
drive.files.create(
{
requestBody: {
name: "sampleDocument",
mimeType: "application/vnd.google-apps.document"
},
media: {
mimeType: "text/plain",
body: fs.createReadStream("./sample.txt")
}
},
(err, res) => {
if (err) {
console.error(err);
return;
}
script.projects.create(
{
requestBody: {
title: "sampleGASProject",
parentId: res.data.id
}
},
(err, res) => {
if (err) {
console.log(err);
return;
}
script.projects.updateContent(
{
scriptId: res.data.scriptId,
auth,
resource: {
files: [
{
name: "Code",
type: "SERVER_JS",
source: 'var obj = {"foo":"bar"}\n'
},
{
name: "appsscript",
type: "JSON",
source:
'{"timeZone":"America/New_York","exceptionLogging":"STACKDRIVER"}'
}
]
}
},
(err, res) => {
if (err) {
console.log(err);
return;
}
console.log("Done.");
}
);
}
);
}
);
- 当您 运行 这个示例脚本时,新的 Google 文档被创建为
sampleDocument
的文件名,新的 GAS 项目被创建为sampleGASProject
的项目名称.- 脚本完成后,您可以在根文件夹中看到新的Google 文档。打开Google文档,打开脚本编辑器,可以看到
var obj = {"foo":"bar"}
. 的脚本
- 脚本完成后,您可以在根文件夹中看到新的Google 文档。打开Google文档,打开脚本编辑器,可以看到
注:
- 如果您使用的是服务帐号,很遗憾,GAS 项目无法使用服务帐号进行管理。请注意这一点。所以请使用 OAuth2.
参考文献:
在我的环境中,我可以确认示例脚本有效。但如果这在您的环境中不起作用,我深表歉意。届时请检查API是否启用and/or其他环境。如果我误解了你的问题,这不是你想要的方向,我深表歉意。