Monday, 9 September 2013

How to debug Google Apps Scripts

How to debug Google Apps Scripts

So in my quest to learn new things I am experimenting with Google Apps
Scripts and am attempting to create a pig-latin translator. However my
results always return undefined
here is my code:
function createAndSendDocument() {
// Get Date and Time
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
// Create a new Report
var doc = DocumentApp.create('Pig Latin Translation:' + datetime);
doc.appendParagraph(ParseText('asdf\n hat\n cat dog\n apple'));
// Save and close the document
doc.saveAndClose();
// Get the URL of the document
var url = doc.getUrl();
// Get the email address of the active user - that's you
var emailAddress = Session.getActiveUser().getEmail();
// Send yourself an email with a link to the document
GmailApp.sendEmail(emailAddress,
'Translation Complete: '+ datetime,
'Here is a link to the translation created by the
script: ' + url);
}
function ParseText(myText)
{
var lines = myText.split("\n");
var results = "";
for(var i = 0, len = lines.length; i < len; i++)
{
var words = lines[i].split(" ");
for(var i = 0, lenght = words.length; i < lenght; i++)
{
var word = words[i];
if (word.charAt(0) =="e"
|| word.charAt(0) =="i"
|| word.charAt(0) =="o"
|| word.charAt(0) =="u"
|| word.charAt(0) =="y"
)
{
results = results + word+"ay ";
}
else
{
var mutated = word.substring(1, word.length-1);
results = results + mutated + word.charAt(0) +"ay ";
}
}
results = results +"\n";
}
}
First, Why do I get undefined as the only text in my newly created
document and Second, how can I set breakpoints in the Google Apps Scripts
development environment?

No comments:

Post a Comment