google-apps-script , Output API data to a csv file

Output API data to a csv file


Tag: google-apps-script

I have Frankenstined bits of code together found on here and on various Google tutorials. My goal is to get data from the Google Analytics API and output it to a file in my Google Drive "my_data.csv".

I am able to get my data from the GA API in a variable "results". The default output of results variable is an object (I think?) and looks like this:


dataTable={cols=[{id=ga:year, label=ga:year, type=string}, {id=ga:month, label=ga:month, type=string}, {id=ga:medium, label=ga:medium, type=string}, {id=ga:source, label=ga:source, type=string}, {id=ga:campaign, label=ga:campaign, type=string}, {id=ga:goal1Completions, label=ga:goal1Completions, type=number}], rows=[{c=[{v=2015}, {v=06}, {v=(none)}, {v=(direct)}, {v=(not set)}, {v=117}]}, {c=[{v=2015}, {v=06}, {v=(not set)}, {v=(not set)}, {v=(not set)}, {v=0}]}, {c=[{v=2015}, {v=06}, {v=(not set)}, {v=ET}, {v=DL}, {v=0}]}, {c=[{v=2015}, {v=06}, {v=(not set)}, {v=a1e88567-3904-4375-a14a-cff82d7613d2}, {v=wda}, {v=0}]},

Or I can change the output 'output':'dataTable' in the API call. The results then look like:


rows=[[2015, 06, (none), (direct), (not set), 117], [2015, 06, (not set), (not set), (not set), 0], [2015, 06, (not set), ET, DL, 0], [2015, 06, (not set), a1e88567-3904-4375-a14a-cff82d7613d2, wda, 0], [2015, 06, (not set), google_plus, DL, 0], [2015, 06, Display, Facebook, Facebook, 0], [2015, 06, Display, GOOGLE, ReTargeting, 0], [2015, 06, ET, Email, SearchTopStories, 1], [2015, 06, affiliate, cj, 1651317, 0],

So there are two ways I can ouput the data. Currently I do not know which is "best" wrt to getting my data into a csv. For now I'm trying with the dataTable structure.

Here is my code:

// set some global vars
var profile = 'ga:XXXXXXXX'; // my actual id added here
var my_csv = 'my_csv';

//trigger function runs everything else
function getToIt() {

    var results = getReportDataForProfile(profile);
    convertRangeToCsvFile_(results, "my_data");

// get GA data
function getReportDataForProfile(profile) {

  var startDate = getLastNdays(1);
  var endDate = getLastNdays(0);

  var optArgs = {
    'dimensions': 'ga:year,ga:month,ga:medium,ga:source,ga:campaign', // Comma separated list of dimensions.
    'start-index': '1',
    'max-results': '10',
    //'samplingLevel': 'higher-precision'

  // Make a request to the API.
  var results = Analytics.Data.Ga.get( // mcf for multi channel api, Ga for core
      profile,                    // Table id (format ga:xxxxxx).
      startDate,                  // Start-date (format yyyy-MM-dd).
      endDate,                    // End-date (format yyyy-MM-dd).
    'ga:goal1Completions', // Comma seperated list of metrics.


  if (results.getRows()) {
    return results;

// child function for setting date range
function getLastNdays(nDaysAgo) {
  var today = new Date();
  var before = new Date();
  before.setDate(today.getDate() - nDaysAgo);
  return Utilities.formatDate(before, 'GMT', 'yyyy-MM-dd');

// convert results to csv
function convertRangeToCsvFile_(data, file_name) {

  Logger.log("data here:" + data);
  var csv = google.visualization.dataTableToCsv(data);  

  //output to drive
  DriveApp.createFile(file_name, csv);
  return csvFile;

The error message I receive just now is

ReferenceError: "google" is not defined

Looks like my issue is right here:

var csv = google.visualization.dataTableToCsv(data); 

How do I create the variable csv? I'm not sure how to import the google.visualization module given my environment is GAS. I'm not even sure that it's the right approach.


Here is a very basic 2d array to csv converter. Also you can't use the JSAPI library on the server side it will not work.

function arrayToCsv(data){
var csv = ""
var eol = '\r\n';
  for(var i = 0; i<data.length;i++){
     csv += data[i].join();
     csv += eol;
  return csv;

function t(){
var data = [[1,2,3],[4,5,6],[7,8,9]];
 data.unshift(["title1","title2","title3"]); // add my header array;
  var newcsv  = arrayToCsv(data);
  var backToData = Utilities.parseCsv(newcsv);



Google Apps Script: Bi-directional sync when rows are deleted

I have two sheets which are synced in Google Sheets using a GAS i found on this SO, which i put on a change trigger Google Apps Script: Is there a way to keep two sheets synchronized? script is here var sourceSpreadsheetID = "ID HERE"; var sourceWorksheetName = "SHEET NAME...

Google Script get active row inside Array

I have this google script to read rows from a google spreadsheet but I need to find out the row number of the active array element. The goal is to check if a row has been processed or not (statusverwerkt variable). If the value of 'statusverwerkt' is 'x' I create...

SpreadsheetApp.getActiveSpreadsheet() is breaking script

I'm writing my 1st google app script. Spreadsheet opens a sidebar in onOpen(). Sidebar has Button and in SidebarJavaScript.html I have listener to that button. I'm calling SpreadsheetApp.getActiveSpreadsheet() inside listener and after this line script is not executing. What could possibly be wrong here? function onScrapeClick(){ // Disable Button this.disabled...

Google Apps Script (Javascript) - findText returns location relative to inline image, not start of paragraph

I am trying to insert an image in the middle of a paragraph from the results of findText, but when there are other images before in the paragraph it doesn't work as planned. Currently, my code(partially based on the responses to this question) works by finding the place that needs...

How do I make a Sidebar display values from cells?

I have a Google Sheet that I'm using as a database of clients. There are quite a lot of variables (columns) that I need to store per entry, so I thought that it would be easier to view each entry if I could have a Sidebar display all of the...

How to implement rangeProtect() over multiple tabs - google spreadsheet

I'd like to implement a script to implement range protections on the different tabs of the spreadsheet when someone create a new spreadsheet by copying my template. The issue I encounter is when the script run through the protection implementation. My code seems to be fine as per the debug...

Calling FormApp within doGet(e) function

I am relatively new to scripting, so please be kind. I am trying to create a function bound to a Form within Google Apps, including a webapp which captures 3 variables (requestNum, approverNum, and status) and obtains various other information from the FormApp (e.g titles, responses, etc). I am receiving...

Nslookup or dig in Google App Script

Is there any way to run an nslookup / dig in Google App Script? I m looking to get the results of this command in a Google Spreadsheet. Thanks...

Google Apps Script: event.setTime error and time format

I'm creating something of a booking system which is nearly complete. Currently I'm collecting data from a form, passing it to a sheet, and then using some of that info to create a calendar event. This all works fine. On creating the event, I'm also collecting the eventID so that...

Publish a Spreadsheet Add-on in the Chrome Store

I'm trying to publish a Spreadsheet Add-on following the the app scripts documentation but I get this error: Please fix the following errors: API Console project with the id specified in the manifest's api_console_project_id field, does not have Google Apps Marketplace SDK enabled. api_console_project_id Here you can see a screenshot:...

Google Form to Document Merge to Pdf to Mail

I think my issue is in the e.values I am using along with the ('keyXXXXX', XXXXX); that I am using. The original code I used was something I borrowed from TJ Houston's site. I am using it for an application process. I wrote the code following the process of using...

Google Apps Script return array values and use them in a javascript function

I am trying return an array and use it in a javascript function, but it doesn't seem to work. My is as follows: function doGet() { return HtmlService.createHtmlOutputFromFile('test') .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function test() { var locations = []; var ss = SpreadsheetApp.openByUrl(''), sheet = ss.getActiveSheet(), range = ss.getRange("D2:D4"), values =...

DriveApp.getFolderById Permissions

I'm trying to "upgrade" an old script from DocsList to DriveApp and having a problem with a getFolderById call. The script had worked previously using DocsList.getFolder. I have a (previously-created) folder named "Email Archive" that I'm trying to access in script. Through using Logs and Execution Transcript, I've verified that...

Script to check for email subject not in inbox runs without error but not as expected. Not sure what is wrong with syntax.

I get an automated email when an event occurs, but need to be notified if it isn't received. It's easy to overlook something that doesn't happen. Here's what I have, but the var c remains 0 when script ends (I'm expecting it to not be 0 if email is found,...

Stylesheet for .html document not imported

I have this form I created in HTML (called form.html) written in google apps script and I also have a stylesheet (CSS) to go with that. All is working well when I have the CSS in the same HTML-file as the form. However if I want to put my stylesheet...

Google Apps Script - Sharing outside of domain

So I have followed the tutorial here, and everything works great... for me. google forms file upload complete example What I need to do is add the script to a Google Form, and allow users to upload an image along with the form. I know it will only put it...

Google Spreadsheet custom function to add csv in spreadsheet

I am using Google spreadsheets, and in my google spreadsheet i want to import data from my RESTful web service. I cant use =IMPORTDATA due to the fact that i need to add a header to the url for auth purposes. This is the custom function i have so far:...

Google sheet app script errors after laptop sleep

I have a custom function in a sheet that calls a mysql database. It extracts one value from mysql per row, e.g. | A | B -------------------------------- 1 | YES | =readCompleteFromMember(A1) 2 | NO | =readCompleteFromMember(A2) If I let my laptop go to sleep and open it the next...

Running Apps Script from Chrome extension requires authorization

I'm trying to execute a Google Apps Script from an event on my Chrome extension. While it works great, when ran for the first time by a new user, the apps script requires for the user to give it authorization before it can execute. The problem is when running the...

Advanced Drive Services in Addons

I have a Google Script that uses the Advanced Drive API service and am now looking to convert this script into an Add-on. However the Drive API needs to be enabled manually by the user before using the service inside the Apps Script. Is it possible to use the advanced...

Google Sheets API for clickable images in Google Sheets

In Google sheets, you can insert a clickable image to a cell which links to a function in app script. Can we also insert this clickable image and link it to app script from our server using Google Spreadsheet API? Or Can we call app script from our server?

Output API data to a csv file

I have Frankenstined bits of code together found on here and on various Google tutorials. My goal is to get data from the Google Analytics API and output it to a file in my Google Drive "my_data.csv". I am able to get my data from the GA API in a...

Pass webapp link parameter to html file in Google Apps Script

I am trying to pass a parameter from a webapp link, so it can be used to pre-fill an html form (using HtmlService). Apps Script - function doGet(e) { var formId = e.parameter.formId; return HtmlService.createTemplateFromFile('Index') .id = formId .evaluate() .setSandboxMode(HtmlService.SandboxMode.IFRAME) } Index.html - <form id="myForm"> <input type="text" name="formId" value="<?=id?>"><br> <input...

How to prevent Google Forms from converting form input to scientific notation format

I have a simple script set up that sends emails based on Google form entries using a script-based VLookup to get the contact emails. In some cases, Google Forms converts longer numbers entered into the form field to scientific notation. A workaround I have been using is to enter an...

Javascript (Google Scripts) value from a function is not returning

I think could be a simple one to solve, I am stuck working this simple issue out. I have called a createEvent function to create a google calendar event. As part of this function I also get the google calendar event ID as EventId and want to return it. For...

How to remove quote mark from array

I have a script that will import data from a csv file in to a Google sheet. I would like to include the following to remove the quote marks from the data - .replace(/"/g, "") - but am not sure of the best place/syntax to add in to the existing...

Access dropdown items in Google App Scripts UiApp

I'm using the older depreciated UiApp class in javascript (because I have to) where I have a dropdown list I need to access the ListBox, and see all the values that it contains. Is there a way to do this? All the items must be present because there is method...

Google spreadsheet script authorisation to BigQuery

I have a Google spreadsheet with a script that connects to BigQuery (using this tutorial - It adds an extra menu option and users can run the script that executes a query to BigQuery. It works fine for me and I want to share this spreadsheet with other users...

Google Spreadsheet setBackgroundColors() “Deprecated”

A gs I have been using in the past to check for duplicates, is no longer working. The script would check all cells in the spread sheet if any were identical it would highlight all their occurrences green. I also had another function that would revert all the cells back...

Merging Multiple Arrays Evenly/Alternating with Javascript and Google AppScript

I'm trying to merge multiple arrays evenly/alternating in javascript/Google appScript. There are several arrays (5 or 6). I've tried 2 different methods, but neither worked. I don't work a lot with javascript honestly and I've managed to get the code to this point, but I can't get it merge properly;...

How to real-time monitor the emails?

Good day! I want to offer the implementation of google apps to my company. Also we have a need to reply on messages, that we recieve through our web-site in 10 minutes time limit. For this purpose our big team of sales managers a handling emails when they have a...

Google Calendar: How to check guest is not busy and available to book through google calendar

I am exploring to write google calendar api integration through google scripts or apis. Is there a way I can check a guest is available before I book an event ?...

How can I exclude a sheet in an array, and sort with a range

Good Morning everyone I have a code that was posted here a while back, I recently started using for a google spreadsheet and tweaked it a bit, how ever there are 2 thigs I have not been able to accomplish the first thing is to Exclude the Master sheet from...

How to get the last entry containing a certain value in Google Spreadsheet

I have an answer spreadsheet linked to a Google form. I wish to get the last date that and answer was submitted. Lets say I my entries look like this : DATA : A B 1 Date String 2 2015-05-09 Abb 3 2015-05-11 Bcc 4 2015-05-12 Cdd 5 2015-05-20 Bcc...

printing spreadsheet to pdf then saving file in drive using oauth2

function topdf() { var foldersave=DriveApp.getFolderById('0Byy1DdsfdfTQRnVlfb05wOV83T00') var d= new Date() var oauthConfig = UrlFetchApp.addOAuthService("google"); var scope = ""; //make OAuth connection oauthConfig.setAccessTokenUrl(""); oauthConfig.setRequestTokenUrl(""+scope); oauthConfig.setAuthorizationUrl("");...

How to get a button from a Google Apps dialog

I must be missing something really stupid here, but I just cannot work to get an interface button from a dialog box in Google Apps. If I try to set the disabled to false in a this object from the "click" function, the button is enabled. But if I try...

Script for Gmail label to Sheets

I am trying to make a script that takes an email within a certain label and puts it into google sheets. Im using this function at the moment: function getMessagesWithLabel() { var destArray = new Array(); var threads = GmailApp.getUserLabelByName('Facebook').getThreads(0,10); for(var n in threads){ var msg = threads[n].getMessages(); var destArrayRow...

getJSON from Google Apps Script not working in Safari

I've created this piece of code to handle my user login. var gasUrl = "[my id]/exec"; function submitFormData() { var username = $("#inputEmail").val(); var password = $("#inputPassword").val(); $.getJSON(gasUrl + "?username=" + username + "&password=" + password, function(data) { if(data[0]) { localStorage.setItem("username", data[0].username); localStorage.setItem("id", data[0].id); window.location.href = "/index.php"; } else {...

Constantly getting an email: Cannot read property “0” from undefined

I am constantly getting emails from "me" with this in it: Cannot read property "0" from undefined. I think it has something to do with Google Apps script because it started when I tried to code something. (for details: I was trying to code a "Contact Us Form" for my...

Google Scripts + Google API PHP

I am trying to call one of my Google Scripts from my google-api-php-client and I can't find how to do this. I deployed my app as a web app, and I got the web app url which I can call with curl, but I need to call it as my...

Get column to display month name Google App Script

How do I set a column to display the name of the month instead of the whole date using the script? The function can manually be performed by selecting Format>Number>August in the menu but I want to do this in code. This is what I've tried but I can't seem...

Delete or Trash specific file in Drive

I had a script that ran every day at 5 am, that would move a specific file (data.xls) to the trash. However, since DocsList has been retired, the script no longer functions, and I'm having trouble updating it. I've seen a couple of delete/setTrashed scripts posted here, but they all...

Break Google Apps HTMLService Page into multiple .html files

I want to combine a few related HTMLService interface in to a tabbed page. I can get the tabs working using the code here. But I want to put the page for each Tab into a different html file in the project. How would I show Billets.html inside the div...

Search for value in variable Google Apps Script/JS

Trying to determine if calendar events in Google Calendar were created by the user himself or if a CSV bulk upload was performed. If the script finds an ID with "CSV" somewhere in the value, then remove it. I tried to add an if statement where it looks at the...

Google Sheets Adds-on open automatically when user opens a sheet or clicks a link in worksheet

I created an google sheets add-on. However, it is a bit hidden in the adds-on menu. Is there a way to enable the Adds-on automatically when user opens a sheet or when user clicks a link in a sheet? I searched on Google Sheets documentation but found nothing. EDIT1: Since...

Output Google Spreadsheet to XML/RSS/Atom using Google AppScript Content Service

So I've searched this site and found nothing really useful so far to accomplish this. I am wanting to turn a spreadsheet into an RSS feed using google script content service. How do I declare column values (i.e. pubDate, Author, Content) so it will output correct RSS/Atom feed. Following their...

Unable to select some text from Google Docs with a simple regex

I'm trying to highlight some text (in the example below I would like to highlight "ORGANIZA") with some regex on a Google Docs document, but I'm unable to make first work a simple regex to find the "category_name" string. Why this: function highlightTextTwo() { /* DOCUMENT DEFINITION */ var doc...

Function to check for first blank cell

so, I'm attempting to define a custom function that returns the row number of the first blank cell it finds within a custom range. function checkForBlank(startRow,startCol) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var checkRow = startRow; var checkCol = startCol; var range = sheet.getRange(checkRow, checkCol); var i...

Edit a Google Spreadsheet using Google Apps Script

I am trying to edit a google spreadsheet using google apps script. Basically, I have a cell in the spreadsheet that has the value "Pending Approval" in it. When the script is executed, I want it to change the value in that cell to "Approved". Is there a simple way...

Google Spreadsheet (Using named range in script)

Trying to get my script to use a named range. In that range I am trying to reset the cell colors back to white. I keep getting an object error, can't figure out how to make this work. function resetCheckDirectory() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var tableRange = ss.getRangeByName("rangename"); tableRange.setBackgrounds("#FFF");...