This week I worked on several Google Apps Scripts projects. Part of coding is failing. You write your code and cross your fingers. Most likely there is something that needs to be “debugged” or fixed. Productive struggle is highly motivating. With programming, you have an end goal and something functional after you get your code to work. This makes something that is challenging – fun!! This is the idea behind many video games. Some levels are very hard to beat, but you get feedback and refigure your approach and try again. Programming is just like that!
Here is a list of some of the Google Apps Scripts projects I have made that you can use!
If you want to see the Google Apps Script code you can go to the Tools menu and choose “Script editor.”
Top to Bottom
One thing to realize about coding is that it runs each line of code one at a time. If there is an error in line 2 the computer does NOT go to line 3.
One way to figure out where you error is is to see what happened last.
Sample
Here are a few of the lines of the code I was doing earlier.
var sss = SpreadsheetApp.setActiveSpreadsheet(followers);
var newss = SpreadsheetApp.getActiveSpreadsheet();
newss.getRange(“A1”).setValue(‘Go to the Add-On Menu and choose the TurnInTab option.’);
newss.getRange(“A2”).setValue(‘Choose Check in.’);
newss.getRange(“A3”).setValue(‘This links your spreadsheet to your leader/teacher.’);
newss.getRange(“A4”).setValue(‘Check In this spreadsheet FIRST’);
newss.getRange(“A5”).setValue(‘After editing your sheet, use Add-On menu to Turn In.’);
As part of the code I was putting text into cells of a spreadsheet. If I do not see the text show up, then I know the error happened before those lines of code. If the text does show up, I look after.
Sometimes I will insert a line of code to insert some text, not because I want the text there, but because it helps me to see if the code is running up to that point. Once I realize my code to that point is good, I delete the insert text code.
Code and Test
I will write a couple of lines of code and then test it to make sure it does what I want it to do. Most of the time it is a capitalization error or I forgot a semicolon. What is wrong can be so small that trying to find the error in many lines of code can be really frustrating.
Comment Everything
Putting two slashes after a line of code allows you to leave a comment. For almost every line add a comment reminding yourself, or anyone who looks at your code, what that line of code is supposed to do!
Example
var leaderUrl = ss.getUrl();//get the Url of this spreadsheet
Comment Out
Putting two slashes in front of a line of code tells the computer this is a comment, not a line of code. If you are having a challenge getting a certain part of your code to work, comment it out and work on it later.
Example
// newss.insertSheet(2).setName(‘template’);//create a tab to edit
//var temp = newss.getSheetByName(‘template’);
//temp.deleteRows(70,1000-70);
//temp.deleteColumns(9,26-9);
Try It
Try my first tutorial on Google Apps Script. See how you do!
1 thought on “Google Apps Script: Debugging Tips from Alice”
There is a built-in debugger and an execution transcript that can also help locate troublesome lines of code.