1

I am trying to create an Apps Script that will auto-convert all gdoc files to docx files (and all gsheet files to xlsx files). Some parts of the puzzle are addressed here: Batch convert Google Docs files to Microsoft Word, however this creates a new file. I need to keep the URL/ID of the original file. I tried using "setContent" on the File API but that doesn't seem to handle blobs. So thats why I resorted to the advanced Drive API. However, I can't seem to get it to work properly. The filename is replaced, the contents are replaced, but the file stays Google Doc, even though I supply a Mime Type. Any ideas?

This is my code:

function convertGoogleDocsToMicrosoftWord() {
  var folderId = "MY_FOLDER_ID"; // Note, eventually I would like to get this from the trigger event somehow so I would welcome ideas on this, too

  var files = DriveApp.getFolderById(srcfolderId).getFilesByType(MimeType.GOOGLE_DOCS);
  while (files.hasNext()) {
    var file = files.next();
    var contents = UrlFetchApp.fetch(
        "https://docs.google.com/document/d/" + file.getId() + "/export?format=docx",
        {
          "headers": {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()},
          "muteHttpExceptions": true
        }
      ).getBlob();

    Drive.Files.update({
      mimeType: MimeType.MICROSOFT_WORD,
      title: file.getName() + '.docx'
    }, file.getId(), contents);
  }
}

Some further questions:

  • assuming I can make this to work, can it update while the file is open?
  • I would like to be able to launch this on trigger events... however standalone scripts can't seem to be able to get a ref to the current folder they are executed in and then recourse. Is this possible?
Rubén
  • 34,714
  • 9
  • 70
  • 166
JoroP
  • 11
  • 1
  • 1
    Your not updating the file stream are you? YOu cant just change the mime type and then poof its a different type. I also dont think your going to get to keep the same id – Linda Lawton - DaImTo Apr 01 '21 at 17:42
  • Try using ````'application/vnd.openxmlformats-officedocument.wordprocessingml.document'```` as mimeType – CMB Apr 01 '21 at 17:55
  • Welcome to [so]. Questions on this site should be specific, meaning only one question per post. – Rubén Apr 02 '21 at 00:11
  • At first, I apologize that my sample script is not useful for your situation. About your question, I understood that you want to convert a Google Document file to a DOCX file. From this situation, I cannot understand about `the original file` of `I need to keep the URL/ID of the original file.`. In this case, what is `the original file`? – Tanaike Apr 02 '21 at 01:14
  • @ DaImTo - I believe I am - the third parameter to the update method has the new contents, so I'm not just changing name and mime type. |||| @ CarlosM - unfortunately, I tried that, it didn't work |||| @ Rubén - Thank you. My question is specific, I've added the ones below as further clarification about what i'm trying to achieve. |||| @ Tanaike - Your script was of great help! What I mean is, it creates a NEW file. I want to have the resulting DOCX file keep the same ID/URL as the GDoc file I am converting, so in essence create a new version of it. – JoroP Apr 02 '21 at 19:02
  • About `I want to have the resulting DOCX file keep the same ID/URL as the GDoc file I am converting, so in essence create a new version of it.`, when you want to give the Google Document ID as the converted DOCX file, I think that unfortunately, that cannot be achieved. Because the file of Google Document is not the same with the file of DOCX file, and also, the mimeType is different. If I misunderstood your goal, I apologize. – Tanaike Apr 03 '21 at 00:39
  • @Tanaike I want to change the contents of the same file (as defined by its ID) with the contents of the conversion to DOCX and to change its metadata to reflect that. It's baffling to me why it shouldn't be possible. – JoroP Apr 03 '21 at 08:26
  • Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand about `I want to change the contents of the same file (as defined by its ID) with the contents of the conversion to DOCX and to change its metadata to reflect that.`. You want to give the Google Document ID to the converted DOCX file. Is my understanding correct? – Tanaike Apr 03 '21 at 11:39
  • @Tanaike apologies for being late with my reply. Yes, this is what I want. It is correct. – JoroP Apr 13 '21 at 16:40
  • Thank you for replying. For my question of `You want to give the Google Document ID to the converted DOCX file. Is my understanding correct?`, I understood that your answer is `Yes, this is what I want. It is correct.`. In this case, it cannot be achieved as I said at my 1st comment because of the specification of Google side. I apologize for this. – Tanaike Apr 14 '21 at 00:40

0 Answers0