Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing to the console in Google Apps Script?

I am very new to programming (have taken some of the JS courses on Codecademy). I am trying to create a simple script to determine, if given a spreadsheet with results from a poker game, who should pay whom. I opened up Google Apps Script, and wrote the following to get started:

function addplayerstoArray(numplayers) {    var playerArray = [];    for (i=0; i<numplayers; i++) {     playerArray.push(i);   } }    addplayerstoArray(7);  console.log(playerArray[3]) 

The idea is to create an array with the total number of players in it. When running the code, I thought it would print "3" to the console. But nothing happened. It said

"ReferenceError: "console" is not defined."

A) What do I not understand about how the Google Apps Script console works with respect to printing so that I can see if my code is accomplishing what I'd like?

B) Is it a problem with the code?

like image 891
jim_shook Avatar asked Sep 11 '12 20:09

jim_shook


1 Answers

The console is not available because the code is running in the cloud, not in your browser. Instead, use the Logger class provided by GAS:

Logger.log(playerArray[3]) 

and then view the results in the IDE under View > Logs...

Here's some documentation on logging with GAS.

Edit: 2017-07-20 Apps script now also provides Stackdriver Logging. View these logs in the script editor under View - Console Logs.

like image 156
Pete Avatar answered Sep 19 '22 14:09

Pete