【问题标题】:Create a Google Document with a built-in object using Node使用 Node 创建带有内置对象的 Google 文档
【发布时间】:2020-04-11 10:36:39
【问题描述】:

我已经看到下面关于如何使用 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
        }
      }

任何帮助将不胜感激

【问题讨论】:

  • 我可以问你关于你的问题吗?您想使用 googleapis 和 Node.js 创建新的 Google 文档。那时,您想将一些文本放入 Google 文档中。我的理解正确吗?而且,在您的脚本中,您是否已经能够创建新的 Google 文档?
  • I also want to pass through an object so when the google doc gets created, it has that object stored in its environment 是什么意思?
  • @Tanaike 不完全是。我希望将对象存储在 Google Doc 应用脚本中。在 Node 中说,我有一个对象 var obj = { “foo”:”bar”}。当 Node 创建一个新的 google doc 实例时,我希望它在 google doc 应用脚本中编写相同的对象。因此,当我打开那个 google 文档并转到它的脚本编辑器时,{ “foo”: “bar”} 已经在那里了。希望这能澄清一些事情。谢谢你们俩审阅我的帖子。

标签: node.js google-docs


【解决方案1】:
  • 您希望通过将 Google Apps 脚本包含为容器绑定脚本来创建新的 Google 文档。
    • 作为示例情况,您希望包含var obj = { “foo”:”bar”} 的脚本。
  • 您希望使用带有 Node.js 的 googleapis 来实现此目的。

如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

在这个答案中,为了实现这一点,我使用了 Google Apps Script API 中的 projects.create 和 projects.updateContent 方法。

流程:

本回答的流程如下。

  1. 创建新的 Google 文档。
  2. 创建新的 GAS 项目作为已创建的 Google 文档的容器绑定脚本。
  3. 将脚本放到创建的 GAS 项目中。

准备工作:

在运行脚本之前,请做好以下准备。

  1. 请在 API 控制台启用 Google Apps 脚本 API。
    • 根据您的问题,我认为您已经在 API 控制台启用了 Drive API。
  2. 请设置https://www.googleapis.com/auth/drivehttps://www.googleapis.com/auth/script.projects的作用域。
  3. 请删除包含访问令牌和刷新令牌的凭证文件。因为这种方式用于将新范围反映到访问令牌。在删除凭据文件后运行脚本时,将运行授权过程。所以请检索代码并使用代码检索访问令牌和刷新令牌。
  4. 请准备一个示例文本文件作为文件名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 文档。打开谷歌文档,打开脚本编辑器,可以看到var obj = {"foo":"bar"}的脚本。

注意:

  • 如果您使用的是服务帐户,很遗憾,GAS 项目无法使用服务帐户进行管理。请注意这一点。所以请使用 OAuth2。

参考资料:

在我的环境中,我可以确认示例脚本有效。但是,如果这在您的环境中不起作用,我深表歉意。届时,请检查是否启用 API 和/或其他环境。如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

【讨论】:

  • 衷心感谢您的彻底回复!我将在接下来的几天里尝试这个,并让你发布进度。非常感谢你为我指明了正确的方向。
  • @Drive2blue 感谢您的回复。在我的环境中,我可以确认示例脚本有效。但如果这在您的环境中不起作用,我深表歉意。
  • 这就像魔术一样!你真是个天才,先生!再次感谢您提供具体、直观的建议。
  • @Drive2blue 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多