Home > Software design >  Is there a way to read and load an .odt file into a variable with DriveApp
Is there a way to read and load an .odt file into a variable with DriveApp

Time:05-28

I have been writing a script thats supposed to take information from several .odt files and insert that into several different spreadsheet cells and im stuck with the question of how to get that text into a variable so i can do some regex stuff to get the actual information i want. Problem is this:

let text = DocumentApp.openById(idString).getBody();

when i execute that, i get this error

Error   
Exception: Invalid argument

I've read here that one of the issues might be that DriveApp ids are not the same as DocumentApp ids, in that case, ive tried getting the proper id first, but in that case, it still doesnt seem to work.

let text = DriveApp.getFileById(idString).getBlob;

now, that technically works, however, i still dont know how to retrieve the body/text/contents (really, however you wanna call it) from the file. ive tried getDataAsString, but that just gets me well, everything but what i care about.

Hopefully someone can help here

CodePudding user response:

Unfortunately, odt file cannot be directly opened as the readable data. So, in this case, in order to retrieve the text from odt data, I would like to propose the following flow.

  1. Convert odt data to Google Document using Drive API.
  2. Open the created Google Document using the Document service (DocumentApp).
  3. Retrieve the text from the Document.

When this flow is reflected in a sample script of Google Apps Script, it becomes as follows.

Sample script:

Before you use this script, please enable Drive API at Advanced Google services.

function myFunction() {
  const fileId = "###"; // Please set the file ID of odt file.

  const id = Drive.Files.copy({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, fileId).id;
  const doc = DocumentApp.openById(id);
  const text = doc.getBody().getText();
  console.log(text);
}
  • When this script is run, the above flow is run. By this, you can retrieve the text data from odt file.
  • The converted document is created as a temporal file in the root folder. You can remove it with DriveApp.getFileById(id).setTrashed(true) after the script was finished.

Reference:

  • Related