2

I was trying to create a function that takes a number of minutes and outputs it in a nice readable format. For instance, FORMATMINUTES(1570) would output 1d2h10m, but I got hung up trying to find a truncate function. I also had to change my lets to vars. Is there a reference that tells you what javascript features are available when writing custom functions?

To simplify I created two functions. First, the Math.trunc() function doesn't seem to exist as it doesn't show up as a suggestion and throws an error when used:

function MYTRUNC(input) {
  return Math.trunc(input);
}

enter image description here

Second I thought that maybe I could use sheets's built-in TRUNC function, but that isn't defined either:

function MYTRUNC2(input) {
  return TRUNC(input)
}

enter image description here

I read google's guide but it says "Custom functions are created using standard JavaScript" but not what version of javascript. Apparently it doesn't support let.

Their app scripts reference has a lot of information on interacting with sheets, but not a basic or complete reference. I realize that in this situation I can use Math.floor which is available or subtract input % 1, but I'd like to know what other idiosyncrasies there might be, and if I can use newer javascript features. Template literals give an error as well, so maybe it's based on an earlier javascript version?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113
  • 2
    I heard *someone* here on SO say that GAS was going to be updated *very soon* to ES6, but I'm not seeing anything about it in a very quick search about it. For now, write in ES5 (or write in esnext and transpile to ES5 later), though not even everything in ES5 is supported. See https://stackoverflow.com/questions/37768501/google-apps-script-javascript-standard-support – CertainPerformance Apr 20 '19 at 07:30
  • I think that CertainPerformance's comment is correct. In order to use the function of ``Math.trunc()``, for example, how about modifying to ``function MYTRUNC(input) {return input < 0 ? Math.ceil(input) : Math.floor(input);}`` using the Polyfill? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc – Tanaike Apr 20 '19 at 07:40
  • 3
    Possible duplicate of [Google Apps Script Javascript Standard Support](https://stackoverflow.com/questions/37768501/google-apps-script-javascript-standard-support) – TheMaster Apr 20 '19 at 17:45

1 Answers1

0

The best reference I have found is from within the online Script Editor's debugger.

Make a breakpoint anywhere in any function. Then run that function with the bug button to start debugging. Next, click on the two buttons at the end of the tool bar: "Show inheritance" and "Show all data".

In the debugger, you should see a this with a "+" to expand it. Expand it and then expand [[prototype]] to see all the builtin App Script objects (GmailApp, etc), then find the next [[prototype]] below those to see the standard JavaScript objects such as Array, Math, Date, etc and you can inspect all of the functions that are available from there.

dwmorrin
  • 2,704
  • 8
  • 17