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?
The code on .gs files of a Google Apps Script project run on the server rather than on the web browser. The way to log messages was to use the Class Logger.
B) Is it a problem with the code?
As the error message said, the problem was that console
was not defined but nowadays the same code will throw other error:
ReferenceError: "playerArray" is not defined. (line 12, file "Code")
That is because the playerArray is defined as local variable. Moving the line out of the function will solve this.
var playerArray = [];
function addplayerstoArray(numplayers) {
for (i=0; i<numplayers; i++) {
playerArray.push(i);
}
}
addplayerstoArray(7);
console.log(playerArray[3])
Now that the code executes without throwing errors, instead to look at the browser console we should look at the Stackdriver Logging. From the Google Apps Script editor UI click on View > Stackdriver Logging.
On 2017 Google released to all scripts Stackdriver Logging and added the Class Console, so including something like console.log('Hello world!')
will not throw an error but the log will be on Google Cloud Platform Stackdriver Logging Service instead of the browser console.
From Google Apps Script Release Notes 2017
June 23, 2017
Stackdriver Logging has been moved out of Early Access. All scripts now have access to Stackdriver logging.
From Logging > Stackdriver logging
The following example shows how to use the console service to log information in Stackdriver.
function measuringExecutionTime() { // A simple INFO log message, using sprintf() formatting. console.info('Timing the %s function (%d arguments)', 'myFunction', 1); // Log a JSON object at a DEBUG level. The log is labeled // with the message string in the log viewer, and the JSON content // is displayed in the expanded log structure under "structPayload". var parameters = { isValid: true, content: 'some string', timestamp: new Date() }; console.log({message: 'Function Input', initialData: parameters}); var label = 'myFunction() time'; // Labels the timing log entry. console.time(label); // Starts the timer. try { myFunction(parameters); // Function to time. } catch (e) { // Logs an ERROR message. console.error('myFunction() yielded an error: ' + e); } console.timeEnd(label); // Stops the timer, logs execution duration. }