我正在尝试通过 Node.js 使用 API 将 Word 文档转换为 Google 文档。单词文档已经在一个文件夹中,我只想将它们转换为谷歌文档。我正在使用v3。
The v3 docs https://developers.google.com/drive/api/v2/v2-to-v3-reference?hl=vi假设为了使用副本转换文件,您需要将转换参数替换为资源主体中的 mimeType。
我不知道该怎么做?
function listFiles(auth) {
const drive = google.drive({version: 'v3', auth});
drive.files.list({
q: "mimeType = 'application/msword'",
pageSize: 100,
fields: 'nextPageToken, files(id, name)',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const files = res.data.files;
if (files.length) {
console.log('Files:');
files.map((file) => {
console.log(`${file.name} (${file.id})`);
drive.files.copy({
fileId: file.id,
'name' : 'Updated File Name',
'mimeType' : 'application/vnd.google-apps.document'
})
});
} else {
console.log('No files found.');
}
});
}
不确定我是否完全理解如何引用资源主体。会感激一头牛吗?
- 您想要使用 Drive API v3 中的 files.copy 方法将 Microsoft Word(doc 文件)转换为 Google 文档。
- 您希望使用 googleapis 和 Node.js 来实现此目的。
- 您已经能够使用 Drive API 获取和放置 Google Drive 的文件。
如果我的理解是正确的,这个答案怎么样?请将此视为几个可能答案之一。
修改要点:
- 我觉得
drive.files.list()
在你的脚本中工作。
- I think that
drive.files.copy()
has a modification part.
- 请包括
name
and mimeType
in requestBody
or resource
.
- 在这种情况下,它使用回调函数来检索错误消息。
修改后的脚本:
From:
drive.files.copy({
fileId: file.id,
'name' : 'Updated File Name',
'mimeType' : 'application/vnd.google-apps.document'
})
To:
drive.files.copy(
{
fileId: file.id,
requestBody: { // You can also use "resource" instead of "requestBody".
name: file.name,
mimeType: "application/vnd.google-apps.document"
}
},
(err, res) => {
if (err) return console.log("The API returned an error: " + err);
console.log(res.data); // If you need, you can see the information of copied file.
}
);
- 在这种情况下,通过删除扩展名来使用 DOC 文件的文件名。并将复制的文件与DOC文件放在同一文件夹中。
Note:
- 在此修改后的脚本中,假设您设置的范围可用于使用
drive.files.copy()
。如果出现与范围相关的错误,请添加https://www.googleapis.com/auth/drive
到范围。
参考:
- 文件:复制 https://developers.google.com/drive/api/v3/reference/files/copy
如果我误解了你的问题并且这不是你想要的方向,我很抱歉。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)