0

I'm attempting to create a material cut list from input entered by a user in a Google Sheets. I'm not receiving any errors except for the last part, where I try to loop over an array of objects and use the objects values to replace the text on a Google Doc. By invoking Object.value() the following error is thrown.

TypeError: Cannot find function value in object function Object() { [native code for Object.Object, arity=1] }.

I've tried using other object methods, but Object.value() is the only one that allows me to get the actual values from jambObj.

//Create jamb cut list
function jambsCutList() {
var numberOfJambCuts;
var sizeOfJambCuts;
var jambOpenings;
var jambs = 1;
var jambObj = {};
var jambArr = [];

//Loop through the first three google sheet colums
for(var i = 5; i < values.length; i++) {
  numberOfJambCuts = values[i][0];
  sizeOfJambCuts = values[i][1];
  jambOpenings = values[i][2];

  jambObj = {cuts: numberOfJambCuts, size: sizeOfJambCuts, openings: 
  jambOpenings};
  jambArr.push(jambObj);
}

//Sort jambArr from largest cut size to smallest
jambArr.sort(function(a, b) {
  return parseInt(b.size) - parseInt(a.size);
});

//Problem Code
//Loop through an array of objects and print all properties to the doc
for (var j = 0; j < jambArr.length; j++){
  body.replaceText('##jambs' + jambs + '##', Object.value(jambArr[j]))
  jambs += 1;
 }
}
pnuts
  • 58,317
  • 11
  • 87
  • 139
AndyHarbin
  • 1
  • 1
  • 1
  • Object.value? Link documentation on such method. – TheMaster Nov 16 '18 at 22:35
  • 3
    Unfortunately, [``Object.values()``](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values) cannot be used at Google Apps Script yet. So how about using ``Object.keys(jambArr[j]).map(function(e){return jambArr[j][e]})`` instead of ``Object.value(jambArr[j])``? But I'm not sure whether the result of modified ``body.replaceText()`` is what you want. If the result was not what you want, can you provide the detail information about the result you want? – Tanaike Nov 16 '18 at 22:37
  • 1
    Yes, `Object.values()` does not work because Google Apps Scripts roughly supports ES5 JavaScript, and this feature was not introduced until later. See: [Which Edition of ECMA-262 Does Google Apps Script Support?](https://stackoverflow.com/questions/17252409/which-edition-of-ecma-262-does-google-apps-script-support) – Dustin Michels Nov 16 '18 at 23:10

1 Answers1

3

As mentioned in the comments, GAS doesn't support Object.values but it does support Object.keys so you can iterate over the keys to get the array of Object values.

Replace

Object.value(jambArr[j])

with

Object.keys(jambArr[j]).map(function(e) {return jambArr[j][e]}).join(", ")
Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43