使用 API 通过 nodejs 使用 drive.files.copy 将 Word 文档转换为 Google 文档 Google Drive API v3
convert a Word doc into a Google doc using the API via nodejs using drive.files.copy convert in v3 of Google Drive API
我正在尝试通过 nodejs 使用 API 将 Word 文档转换为 Google 文档。 word docs 已经在一个文件夹中,我只想将它们转换为 google docs。我正在使用 v3.
v3 docs 说,为了使用复制转换文件,您需要用资源正文中的 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 文档。
- 您想使用带有 Node.js 的 googleapis 来实现此目的。
- 您已经能够使用驱动器 API 为 Google 驱动器获取和放置文件。
如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。
修改点:
- 我认为
drive.files.list()
在您的脚本中有效。
- 我认为
drive.files.copy()
有修改部分。
- 请在
requestBody
或resource
中包含name
和mimeType
。
- 在这种情况下,它使用回调函数来检索错误消息。
修改后的脚本:
从:
drive.files.copy({
fileId: file.id,
'name' : 'Updated File Name',
'mimeType' : 'application/vnd.google-apps.document'
})
到:
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文件相同的文件夹中。
注:
- 在这个修改后的脚本中,假设您设置的范围可以用于
drive.files.copy()
。如果发生与范围相关的错误,请将 https://www.googleapis.com/auth/drive
添加到范围。
参考文献:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
我正在尝试通过 nodejs 使用 API 将 Word 文档转换为 Google 文档。 word docs 已经在一个文件夹中,我只想将它们转换为 google docs。我正在使用 v3.
v3 docs 说,为了使用复制转换文件,您需要用资源正文中的 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 文档。
- 您想使用带有 Node.js 的 googleapis 来实现此目的。
- 您已经能够使用驱动器 API 为 Google 驱动器获取和放置文件。
如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。
修改点:
- 我认为
drive.files.list()
在您的脚本中有效。 - 我认为
drive.files.copy()
有修改部分。- 请在
requestBody
或resource
中包含name
和mimeType
。 - 在这种情况下,它使用回调函数来检索错误消息。
- 请在
修改后的脚本:
从:drive.files.copy({
fileId: file.id,
'name' : 'Updated File Name',
'mimeType' : 'application/vnd.google-apps.document'
})
到:
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文件相同的文件夹中。
注:
- 在这个修改后的脚本中,假设您设置的范围可以用于
drive.files.copy()
。如果发生与范围相关的错误,请将https://www.googleapis.com/auth/drive
添加到范围。
参考文献:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。