0
votes

When using google scripts to turn a spreadsheet into an xml document, where does google scripts send the finished product?

I have been searching the XMLService documentation page, and I can't find it anywhere.

In the sample code, they show:

var document = XmlService.createDocument(root);

var xml = XmlService.getPrettyFormat().format(document);

Logger.log(xml);

Which seems like they just print the entire xml document into the script log? I tried it, and it really seems like that's what they're doing.

I'm looking for a function that, when called, will create a new document in my google drive called "whatever.xml", but it doesn't appear to exist?

What am I missing?

I saw ContentService has a text output that will download the file to your computer when it runs the script, but this is a script I need to

1)have run automatically every night

2)be stored online in a shared drive

and I'd rather not have to re-upload the file manually when the script runs just so the final output is accesible to the project collaborators (this is a script that I will have run on a timer at midnight every day.) (Also, will it still download the file if I don't have a browser window open when the script runs?)

1
I'm guessing that you will want to build a webapp and use a doGet() and return the xml document with ContentServiceCooper
Have you researched DriveApp? See tag info page for official documentationTheMaster

1 Answers

0
votes

You can use the DriveApp class to create a file in your Drive.

For example:

function createAndSaveXml() {
  // Create xml element
  var root = XmlService.createElement('threads');

  // Get some data for example from your Gmail inbox 
  var threads = GmailApp.getInboxThreads();
  for (var i = 0; i < threads.length; i++) {
    var child = XmlService.createElement('thread')
    .setAttribute('messageCount', threads[i].getMessageCount())
    .setAttribute('isUnread', threads[i].isUnread())
    .setText(threads[i].getFirstMessageSubject());
    root.addContent(child);
  }

  // Create xml document
  var document = XmlService.createDocument(root);

  // Pretty print the xml document
  var xml = XmlService.getPrettyFormat().format(document);

  // Save it to Drive at the root folder
  DriveApp.createFile('document.xml', xml);
}

You will see a file named "document.xml" in your Drive with the xml content.


Recommended reading: