Why are my objects being created with equals signs rather than semicolons / object literal notation?

104 views
Skip to first unread message

Davis Jones

unread,
May 22, 2019, 3:39:59 PM5/22/19
to Google Apps Script Community
Hi GASsers!
I'm having the most difficult time figuring out how to get my constructor to create an array of my draft email ids / subjects in object literal notation. 

Specifically, I've created my constructor that puts the subject and id of each email draft as an object into an array:
function draft(id, subject) {
  this.emailId = id;
  this.emailSubject = subject;
}


Then, when I use this constructor to put a few emails into the array (the code below is using data I manually set as a test)....
function seeDraftMessages() {
  var testArray = [];
  testArray.push(new draft(12412, 'test subject 1'));
  testArray.push(new draft(75162, 'test subject 2'));
  var testArrayDetails = [];
  testArray.forEach(function(msg) {
    Logger.log(msg);
  });
};

I get this as the output in Logs:
[19-05-22 14:35:06:863 CDT] {emailId=12412.0, emailSubject=test subject 1}
[19-05-22 14:35:06:864 CDT] {emailId=75162.0, emailSubject=test subject 2}

The problem is that these objects are not in object literal notation, so it's hard for me to work with them in my HTML interface. I need them to be formatted like this, instead:
[19-05-22 14:35:06:863 CDT] {emailId: '12412', emailSubject: 'test subject 1'}
[19-05-22 14:35:06:864 CDT] {emailId: '75162', emailSubject: 'test subject 2'}

Any help would be greatly appreciated!

Davis

Tanaike

unread,
May 22, 2019, 7:40:35 PM5/22/19
to Google Apps Script Community
In Google Apps Script, "{emailId=12412.0, emailSubject=test subject 1}" means that the value is an object. So, although I'm not sure whether this is the result you want, how about modifying from `Logger.log(msg)` to `Logger.log(JSON.stringify(msg))`?

Davis Jones

unread,
May 22, 2019, 7:54:12 PM5/22/19
to google-apps-sc...@googlegroups.com
Thanks for the reply, Tanaike. When you JSON.stringify each object, you turn both the key and the value into strings, so you get something like:

{"emailId":"12412", "emailSubject":"test subject 1"}

This makes it impossible to reference the key as a property of the object when it's bright into HTML.

On Wed, May 22, 2019 at 6:40 PM Tanaike <kanshi...@gmail.com> wrote:
In Google Apps Script, "{emailId=12412.0, emailSubject=test subject 1}" means that the value is an object. So, although I'm not sure whether this is the result you want, how about modifying from `Logger.log(msg)` to `Logger.log(JSON.stringify(msg))`?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/fbcc58ab-687a-49ae-a140-2ac5a52a70d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tanaike

unread,
May 22, 2019, 8:11:58 PM5/22/19
to Google Apps Script Community
Although I'm not sure about your whole situation, when you want to use the value of `{"emailId":"12412", "emailSubject":"test subject 1"}` as an object, how about using `JSON.parse()`?

Davis Jones

unread,
May 22, 2019, 8:18:08 PM5/22/19
to google-apps-sc...@googlegroups.com
Fantastic suggestion! I will give it a try and let you know how it goes. 

On Wed, May 22, 2019 at 7:12 PM Tanaike <kanshi...@gmail.com> wrote:
Although I'm not sure about your whole situation, when you want to use the value of `{"emailId":"12412", "emailSubject":"test subject 1"}` as an object, how about using `JSON.parse()`?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.

Adam Morris

unread,
May 22, 2019, 8:51:35 PM5/22/19
to google-apps-sc...@googlegroups.com
Confusion here is that the logger is what the logger is outputting.

Logger.log(JSON.stringify(msg)); // will produce what you're looking for

However, you can use dot notation to access the properties directly on the object:

Logger.log(msg.emailSubject);  // test subject 1

So, in sum, the logger output isn't what you're expecting to see, but the code is doing what you're expecting it to.

I highly recommend using the following to inspect objects:

Logger.log(JSON.stringify(msg, null, 4)); // pretty print

————————————————————————————

Adam Morris | IT Systems & English Teacher | IGB International School
Jalan Sierramas Utama, Sierramas,
47000 Sungai Buloh, Selangor DE, Malaysia

t    +60 3 6145 4688
f    +60 3 6145 4600
w   www.igbis.edu.my
e    adam....@igbis.edu.my

————————————————————————————


Alan Wells

unread,
May 22, 2019, 10:59:01 PM5/22/19
to Google Apps Script Community
The Apps Script code editor probably isn't written in JavaScript.  Different programming languages have different ways of notating object structure.  That's probably why the log print out looks that way.  Instead of typing JSON.stringify(obj) every time that you want to log out an obj, you could create a function for logging

function ll(a,b) {
 
//Logger.log('ll ran: ' + a + " " + b)
 
if (ll.arguments.length === 2) {//two arguments were passed in
    b
= processValForLog(b);
   
var f = new Function('a','b','Logger.log(a + ":" + b)');
    f
(a,b);
 
} else {
   
var f = new Function('a','Logger.log(a)');
    f
(a);
 
}
}

window
.processValForLog = function(val) {
try{
 
var typeOfThisVal;
 
  typeOfThisVal
= typeof val;

 
if (val) {
   
if (typeOfThisVal === 'object') {//The variable val can be an object and still be null
     
     
if (Array.isArray(val)) {//The very first test should be for an array because that is easy to check for
       
//Logger.log('it is an array')
       
if (val.toString().indexOf("[object Object]") !== -1) {
          val
= JSON.stringify(val);
       
} else {
          val
= val.toString();
       
}
       
//Logger.log('val: ' + val)
     
} else {//It is an object but not an array
       
//Logger.log('its NOT an array')
       
try{
          val
= JSON.stringify(val);//Test for whether it is a date
       
}catch(e) {//If this is an invalid date then JSON stringify will fail
         
//Logger.log('Error stringifying')
       
}
     
}
     
     
//Logger.log('typeof val: ' + typeof val)
     
       
if (typeof val !== 'string') {
         
try{
            val
= val.toString();
         
}catch(e) {
            val
= e.message;
         
}
       
}
     
     
if (val.indexOf("{") !== 0 && typeof val !== 'string') {
        val
= val.toString();
     
}
     
     
//continue;
   
}
 
}
 
 
if (typeOfThisVal === 'number') {
    val
= '"' + val.toString();
 
}
 
 
if (val === undefined) {//Avoid having all three cells undefined
    val
= 'UNDEFINEDDD';
 
} else if (val === null) {
    val
= 'NULLLL';
 
} else if (val === false) {
    val
= 'FALZZE';
 
} else if (val === true) {
    val
= 'TREWWW';
 
}
 
 
return val;
}catch(e) {
 
var f = new Function('e','Logger.log(e.message + ":\n\n" + e.stack)');
  f
(e);
}
}

Adam Morris

unread,
May 23, 2019, 12:51:22 AM5/23/19
to google-apps-sc...@googlegroups.com
I too actually use a library to help me with this.
All I have to do is:

obj.__print__;  // outputs to log 
obj.__pprint__;  // same as above, but pretty

So if obj = {one: 'one'}:

obj.__print__;  // <{one: 'one'}> [Object]

If obj = [1, 2, 3];
obj.__print__;  // <[1, 2]> [Array (2)] 

If obj = "This is a string"
obj.__print__;  // <"This is a string"> [String]

————————————————————————————

Adam Morris | IT Systems & English Teacher | IGB International School
Jalan Sierramas Utama, Sierramas,
47000 Sungai Buloh, Selangor DE, Malaysia

t    +60 3 6145 4688
f    +60 3 6145 4600
w   www.igbis.edu.my
e    adam....@igbis.edu.my

————————————————————————————

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.

Davis Jones

unread,
Jun 3, 2019, 5:49:54 PM6/3/19
to google-apps-sc...@googlegroups.com
Thanks so much, Adam. I appreciate your response and will be using your library starting today!

Reply all
Reply to author
Forward
0 new messages