Reputation: 579
I need to convert the active Google document into a Word document within a specific folder using the below oAuth scopes.
"https://www.googleapis.com/auth/documents.currentonly"
"https://www.googleapis.com/auth/drive.file"
This is my implemented script.
function convertGoogleDocToWordDoc() {
var rootFolder = findFolderOrFile("MyTest",'application/vnd.google-apps.folder');
if(rootFolder!= null){
folderID = rootFolder;
}
else{
var rFolder = {
title: "MyTest",
mimeType: 'application/vnd.google-apps.folder'
};
var folder = Drive.Files.insert(rFolder);
folderID = folder.getId();
}
var documentObject = DocumentApp.getActiveDocument();
var token = ScriptApp.getOAuthToken();
var ext = ".docx";
// Fetch the docx blob
var blb = UrlFetchApp.fetch("https://docs.google.com/feeds/download/documents/export/Export?id=" + documentObject.getId()+ "&exportFormat=docx",
{
headers : {
Authorization : 'Bearer '+token
},
muteHttpExceptions: true
}).getBlob();
var filename = documentObject.getName();
var name = ~filename.indexOf(ext) ? filename : filename + ext;
var file = {
title: name,
mimetype :'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
parents: [{'id':folderID}]
}
var file = Drive.Files.insert(file,blb);
return file.getId();
}
This creates a Word document in the correct format within the "MyTest" folder only when I use the "https://www.googleapis.com/auth/documents"
oAuth scope.
How should I modify this script to create the Word document using the "https://www.googleapis.com/auth/documents.currentlyonly"
scope ?
Thanks in advance!
Upvotes: 1
Views: 490
Reputation: 201553
For my following asking,
When I tested your script with the scopes of
"https://www.googleapis.com/auth/documents.currentonly","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/script.external_request"
, no error occurs. The DOCX file converted from Google Document is put to the folder. So, when you ran your script, can I ask you about the error message?
I got the following reply,
Yes, it doesn't give errors, but the creating .docx file is corrupted. Try opening the created .docx file.
From your reply and your showing script, I thought that your current situation might be related to your Google Document of var documentObject = DocumentApp.getActiveDocument()
and the scope of https://www.googleapis.com/auth/drive.file
.
If your Google Document of var documentObject = DocumentApp.getActiveDocument()
is not created by the same client with https://www.googleapis.com/auth/drive.file
, the Google Document cannot be obtained. By this, I thought that in the case of your script, your this situation it doesn't give errors, but the creating .docx file is corrupted.
might occur.
But, this is just my guess. So, in order to confirm my guess, please test the following modified script using your Google Apps Script project which has your showing script.
function sample() {
var token = ScriptApp.getOAuthToken();
// 1. Create new Google Document using the scope of `https://www.googleapis.com/auth/drive.file`
var doc = Drive.Files.insert({ title: "sampleDocument", mimeType: MimeType.GOOGLE_DOCS }, Utilities.newBlob("sample text", MimeType.HTML));
// 2. Export Google Document as DOCX.
var blob = UrlFetchApp.fetch(
`https://docs.google.com/feeds/download/documents/export/Export?id=${doc.id}&exportFormat=docx`,
{ headers: { Authorization: 'Bearer ' + token } }
).getBlob();
Drive.Files.insert({ title: "convertedDOC.docx" }, blob);
}
"https://www.googleapis.com/auth/documents.currentonly","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/script.external_request"
.sample text
in the exported DOCX file. This DOCX is not broken.As a workaround for achieving your goal, I would like to propose using Web Apps as a wrapper API for exporting Google Document. Your current situation is as follows.
"https://www.googleapis.com/auth/documents.currentonly","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/script.external_request"
are used.it doesn't give errors, but the creating .docx file is corrupted.
occurs.In order to use Web Apps, please create a new Google Apps Script project. In this case, it is not required to modify appsscript.json
.
Please copy and paste the following script to the script editor of the created Google Apps Script project.
function doGet(e) {
const { documentId } = e.parameter;
var blob = UrlFetchApp.fetch(
`https://docs.google.com/feeds/download/documents/export/Export?id=${documentId}&exportFormat=docx`,
{ headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() } }
).getBlob();
var obj = Drive.Files.insert({ title: "convertedDOC.docx" }, blob);
return ContentService.createTextOutput(obj.id);
// DriveApp.getFiles(); // This is used for automatically detecting the scope for exporting Document. But, from `Drive.Files.insert`, this might not be required to be used.
}
Please enable Drive API at Advanced Google services.
The detailed information can be seen at the official document.
https://script.google.com/macros/s/###/exec
.
Please copy and paste the following script to the script editor of the Google Apps Script project with the scopes of "https://www.googleapis.com/auth/documents.currentonly","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/script.external_request"
. And, please set your Web Apps URL to https://script.google.com/macros/s/###/exec
.
function sample2() {
var documentId = DocumentApp.getActiveDocument().getId();
var webAppsUrl = "https://script.google.com/macros/s/###/exec";
var res = UrlFetchApp.fetch(webAppsUrl + "?documentId=" + documentId);
console.log(res.getContentText())
}
Upvotes: 1
Reputation: 714
I believe it is just not possible. You must use https://www.googleapis.com/auth/documents
as scope, otherwise instead of the document contents, a HTML string containing an Error (Sorry, the file you have requested does not exist.
) is returned by the export
endpoint.
According to the best practices in the documentation, I’d recommend for you to consider using Drive API v3 method Files: export().
Running the method above on my end (without changes), the minimum OAuth scopes to properly save the document as Docx are:
"oauthScopes": [
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/drive.file"
]
Upvotes: 0