将文档导出到本地 google api 时出现问题
Problems exporting document to local google apis
我在通过 googleapi 节点从 google 驱动器下载文件时遇到了一些真正的问题。
这是我目前正在编写的代码
drive.files.export(
{
auth: this.jwtToken,
fileId: fileId,
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
},
async function (err, file, response)
{
console.log(file.data);
}
)
输出的文本如下:
�0$Ҡċ�E��4���G��[�V�%�;]�4,횕9�M������;
��
o����(��* S��Ca��VJ��e\a����}8x%3��a֩��3��L��4f��4��:��
但是当我使用 google 示例中的代码时,如下所示:
https://developers.google.com/drive/api/v3/manage-downloads
我收到以下错误:
TypeError: drive.files.export(...).on is not a function
当我尝试从我的方法中保存字符串时,当我尝试打开它时得到一个损坏的文件。
我做错了什么?
我相信你的目标如下。
- 您想使用 googleapis 将 Google 文档导出为 Microsoft DOCX 格式 Node.js。
- 您已经可以使用 Drive API.
获取文件内容
问题:
不幸的是,我认为官方文档中Node.js的脚本可能没有更新。由此,发生错误。当您为 Node.js(现在是 googleapis@59.0.0
)使用最新版本的 googleapis 时,下面的示例脚本怎么样?
示例脚本:
const fileId = "###"; // file ID of Google Document.
const dest = fs.createWriteStream("###"); // Filename. Google Document is saved as the DOCX format to this file.
drive.files.export(
{
auth: this.jwtToken,,
fileId: fileId,
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
},
{ responseType: "stream" },
function (err, response) {
if (err) {
console.log(err);
return;
}
response.data
.on("end", function () {
console.log("Done.");
})
.on("error", function (err) {
console.log("Error during download", err);
return process.exit();
})
.pipe(dest);
}
);
注:
- 在这个答案中,假设您使用的是 Node.js 的最新版本的 googleapis。目前阶段是
googleapis@59.0.0
。请注意这一点。
- 如果要下载除Google Docs 文件(Document、Spreadsheet、Slides 等)之外的文件,请使用get 方法而不是export 方法。
参考:
我在通过 googleapi 节点从 google 驱动器下载文件时遇到了一些真正的问题。
这是我目前正在编写的代码
drive.files.export(
{
auth: this.jwtToken,
fileId: fileId,
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
},
async function (err, file, response)
{
console.log(file.data);
}
)
输出的文本如下:
�0$Ҡċ�E��4���G��[�V�%�;]�4,횕9�M������;
�� o����(��* S��Ca��VJ��e\a����}8x%3��a֩��3��L��4f��4��:��
但是当我使用 google 示例中的代码时,如下所示: https://developers.google.com/drive/api/v3/manage-downloads
我收到以下错误:
TypeError: drive.files.export(...).on is not a function
当我尝试从我的方法中保存字符串时,当我尝试打开它时得到一个损坏的文件。
我做错了什么?
我相信你的目标如下。
- 您想使用 googleapis 将 Google 文档导出为 Microsoft DOCX 格式 Node.js。
- 您已经可以使用 Drive API. 获取文件内容
问题:
不幸的是,我认为官方文档中Node.js的脚本可能没有更新。由此,发生错误。当您为 Node.js(现在是 googleapis@59.0.0
)使用最新版本的 googleapis 时,下面的示例脚本怎么样?
示例脚本:
const fileId = "###"; // file ID of Google Document.
const dest = fs.createWriteStream("###"); // Filename. Google Document is saved as the DOCX format to this file.
drive.files.export(
{
auth: this.jwtToken,,
fileId: fileId,
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
},
{ responseType: "stream" },
function (err, response) {
if (err) {
console.log(err);
return;
}
response.data
.on("end", function () {
console.log("Done.");
})
.on("error", function (err) {
console.log("Error during download", err);
return process.exit();
})
.pipe(dest);
}
);
注:
- 在这个答案中,假设您使用的是 Node.js 的最新版本的 googleapis。目前阶段是
googleapis@59.0.0
。请注意这一点。 - 如果要下载除Google Docs 文件(Document、Spreadsheet、Slides 等)之外的文件,请使用get 方法而不是export 方法。