- You want to run the function of Google Apps Script after the DOC file (Microsoft Doc file) was uploaded as Google Document.
- You want to use googleapis with Node.js for uploading the DOC file
- You want to use Google Apps Script for the post process of the converted Google Document.
- You have already been able to upload the DOC file with Drive API.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
In this answer, I would like to run the function of Google Apps Script using Web Apps after the file was uploaded. Also you can run the function of Google Apps Script using Apps Script API. But in this case, you are using OAuth2. So I thought that using Web Apps is easy to safe to achieve your goal. The flow of this sample script is as follows.
Flow:
- Upload the DOC file as Google Document using googleapis.
- Run the function of Google Apps Script by requesting to Web Apps.
Usage:
In order to use this, please do the following flow.
1. Create new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
From your question, I could confirm that you have already had the GAS project. So please use it.
2. Copy and paste script.
Please copy and paste the following script.
function doGet(e) {
var fileId = e.parameter.id;
return ContentService.createTextOutput("Done.");
}
3. Deploy Web Apps.
- On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
- Select "Me" for "Execute the app as:".
- Select "Only myself" for "Who has access to the app:".
- In this settings, the access token is used for accessing to Web Apps.
- When "Anyone, even anonymous" is set to "Who has access to the app:", the access token is not required to be used.
- Click "Deploy" button as new "Project version".
- Automatically open a dialog box of "Authorization required".
- Click "Review Permissions".
- Select own account.
- Click "Advanced" at "This app isn't verified".
- Click "Go to ### project name ###(unsafe)"
- Click "Allow" button.
- Click "OK".
- Copy the URL of Web Apps. It's like
https://script.google.com/macros/s/###/exec
.
- When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
4. Modified script:
When your script is modified, please modify as follows. And please set the URL of Web Apps.
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) {
console.error(err);
} else {
const fileId = file.data.id;
console.log("File Id:", fileId);
const request = require("request");
auth.getRequestHeaders().then(authorization => {
request(
{
url: `https://script.google.com/macros/s/###/exec?id=${fileId}`,
method: "GET",
followAllRedirects: true,
headers: authorization
},
function(err, res, body) {
console.log(body);
}
);
});
}
}
);
}
- In your script, the file ID can be directly retrieved using
file.data.id
.
- When the script of Web Apps is finished,
Done.
is returned.
Note:
- When you modified the Google Apps Script of Web Apps, please redeploy Web Apps as new version. By this, the latest script is reflected to Web Apps. Please be careful this.
References:
If I misunderstood your question and this was not the direction you want, I apologize.