1

I'm running a Google Apps Script (based on Gmail data) and I want to save some data from Javascript to local computer. How is it possible to save/download some data, computed in Javascript here:

enter image description here

to a local file?

All I have found is:

var addresses = [];
var threads = GmailApp.search("label:mylabel", 0, 10);
for (var i = 0; i < threads.length; i++) {
  var messages = threads[i].getMessages();
  for (var j = 0; j < messages.length; j++) {
    addresses.push(messages[j].getFrom());
  }
}
Logger.log(addresses);

but Logger.log(...) is not very useful to save this data to local computer.

Basj
  • 41,386
  • 99
  • 383
  • 673

3 Answers3

3

I propose this as an other answer.

If I got data from Google Apps Script and Google Drive, I think that Web Apps can be used for the situation. For example, it retrieves data from Google as a trigger run=ok, a following sample script can be used.

Script :

function doGet(e) {
  if (e.parameter.run == "ok") {

    // do something

    var result = "sample data"
    return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.TEXT);
  }
  return
}

You can retrieve data of result using curl on your local PC as follows. The URL is Web Apps URL.

curl -L https://script.google.com/macros/s/#####/exec?run=ok

Result :

sample data
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks @Tanaike! I usually edit my script in `https://script.google.com/d/...8DSQD-PVKj/edit`, how to get the URL for `curl` with this? Also, is the name `doGet(e)` mandatory or can I use `function myFunction(e) { }` ? – Basj Sep 08 '17 at 10:15
  • @Basj You can retrieve this URL of ``https://script.google.com/macros/s/###/exec?run=ok`` using ``ScriptApp.getService().getUrl()`` at GAS. The URL is constant. So you can continue to use it by retrieving it with one time. And at Web Apps, the function of ``doGet(e)`` has to be used. If you want to other function, you can use ``function doGet(e){return myFunction(e)}``. I misunderstand your comments, I'm sorry. – Tanaike Sep 09 '17 at 00:48
  • Thanks for your answers, it helped me :) – Basj Sep 10 '17 at 14:00
  • Related: https://stackoverflow.com/questions/46141589/google-apps-script-web-app-automatically-linked-to-latest-code. – Basj Sep 10 '17 at 19:12
  • Thanks again @Tanaike, here is the result now: [Get all email addresses associated to a specific Gmail label](https://webapps.stackexchange.com/questions/109627/get-all-email-addresses-associated-to-a-specific-gmail-label). – Basj Sep 10 '17 at 19:49
  • @Basj Thank you for the additional information. I couldn't answer it because of the time difference. I'm sorry. – Tanaike Sep 10 '17 at 22:00
  • @Basj In the case of Web APPs with dev mode, when users use Web APPs from outside, even if the access is "Anyone, even anonymous", login to Google is required. So the Web APPs with dev mode cannot be used using curl. In that case, please use the current web apps URL. – Tanaike Sep 10 '17 at 22:13
0

A Google Apps Script can't save anything directly to your computer. But it can save data to a Google Spreadsheet, which you can then download.

You may want to either use an existing spreadsheet, or create a new one. So, the logic begins either with

var ss = SpreadsheetApp.create("Output");  // new spreadsheet

or

var ss = SpreadsheetApp.openByUrl(url);  // existing spreadsheet

Either way, suppose you want to store the list of addresses in Column A of the first sheet in this spreadsheet. That would be

var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, addresses.length, 1); 
var data = addresses.map(function(x) {return [x];});
range.setValues(data); 

The intermediate step with data is to turn the 1-dimensional array addresses into two-dimensional one, [[addr1], [addr2], ...] - this is the way that values are represented in Google Sheets.

0

Last night I was having some trouble debugging a program and I wanted to be able to view the json in the new UltraEdit on my computer so this is how I got the data from my google script to my computer.

function getText(obj)
{
  var presentationId=getPresentationId();
  var data=Slides.Presentations.Pages.get(obj.presId, obj.pageId);
  myUtilities.saveFile(data)
  for (var i=0;i<data.pageElements.length;i++)
  {
    if(data.pageElements[i].shape && data.pageElements[i].shape.text && data.pageElements[i].shape.text.textElements.length) 
    {
      for(var j=0;j<data.pageElements[i].shape.text.textElements.length;j++)
      {
        if(data.pageElements[i].shape.text.textElements[j].textRun)
        {
          var text=data.pageElements[i].shape.text.textElements[j].textRun.content;
        }
      }
    }
  }
  return text;
}

myUtilities.saveFile(data) is part of a utilities library I wrote that makes it easy to store all kinds of data in ascii files. It just takes a few minutes and the file is auto synced down to my computers Google Drive and I opened the file with UltraEdit and begin analyzing it. Nothing remarkable. No fancy foot work just taking advantage of what's already there.

Cooper
  • 59,616
  • 6
  • 23
  • 54