- 您希望在将 DOC 文件(Microsoft Doc 文件)作为 Google 文档上传后运行 Google Apps Script 的功能。
- 您想使用 googleapis 和 Node.js 来上传 DOC 文件
- 您想使用 Google Apps 脚本对转换后的 Google 文档进行后期处理。
- 您已经能够使用 Drive API 上传 DOC 文件。
如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。
在这个答案中,我想在文件上传后使用 Web Apps 运行 Google Apps 脚本的功能。您也可以使用 Apps Script API 运行 Google Apps Script 的功能。但在这种情况下,您使用的是 OAuth2。所以我认为使用 Web 应用程序很容易安全地实现您的目标。该示例脚本的流程如下。
流程:
- 使用 googleapis 将 DOC 文件作为 Google 文档上传。
- 通过向 Web Apps 请求运行 Google Apps Script 的功能。
用法:
为了使用它,请执行以下流程。
1。创建 Google Apps 脚本的新项目。
Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。
根据您的问题,我可以确认您已经拥有 GAS 项目。所以请使用它。
2。复制并粘贴脚本。
请复制并粘贴以下脚本。
function doGet(e) {
var fileId = e.parameter.id; // You can retrieve the file ID from Node.js using this.
// do something: Please set the function you want to run.
return ContentService.createTextOutput("Done.");
}
3。部署 Web 应用程序。
- 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
- 为“执行应用程序为:”选择“我”。
- 为“谁有权访问应用程序:”选择“仅限我自己”。
- 在此设置中,访问令牌用于访问 Web 应用程序。
- 当“任何人,甚至匿名”设置为“谁有权访问应用程序:”时,不需要使用访问令牌。
- 单击“部署”按钮作为新的“项目版本”。
- 自动打开“需要授权”对话框。
- 点击“查看权限”。
- 选择自己的帐户。
- 点击“此应用未验证”中的“高级”。
- 点击“转到###项目名称###(不安全)”
- 点击“允许”按钮。
- 点击“确定”。
- 复制 Web 应用程序的 URL。就像
https://script.google.com/macros/s/###/exec。
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
4。修改后的脚本:
当您的脚本被修改时,请进行如下修改。并且请设置 Web Apps 的 URL。
function uploadFile(auth) {
const drive = google.drive({ version: "v3", auth });
var fileMetadata = {
name: "My Uploaded Test Word File",
mimeType: "application/vnd.google-apps.document"
};
var media = {
mimeType: "application/msword",
body: fs.createReadStream("./test-word.doc")
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id"
},
function(err, file) {
if (err) {
// Handle error
console.error(err);
} else {
const fileId = file.data.id;
console.log("File Id:", fileId);
// The below script requests to Web Apps.
const request = require("request");
auth.getRequestHeaders().then(authorization => {
request(
{
url: `https://script.google.com/macros/s/###/exec?id=${fileId}`, // Please set this.
method: "GET",
followAllRedirects: true,
headers: authorization
},
function(err, res, body) {
console.log(body);
}
);
});
}
}
);
}
- 在您的脚本中,可以使用
file.data.id 直接检索文件ID。
- Web Apps脚本执行完毕,返回
Done.。
注意:
- 当您修改 Web Apps 的 Google Apps 脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。
参考资料:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。