An error will occur if you try to define a global constant in Google Apps Script.

const neko = "neko";
function myFunction () {
  var idSS = SpreadsheetApp.create ("test"). getId ();
  var fileSS = DriveApp.getFileById (idSS);
  Logger.log (fileSS);

When the above code is executed, the error "TypeError: Redeclaration of constant neko" is displayed.

However, ifconst neko = "neko";on the first line is moved into myFunction, no error will occur.

Can Google Apps Script not define constants in the global domain?

Thanks for your professor.

  • Answer # 1

    Unfortunately,constis currently not available globally. This seems to be a specification. Therefore,const neko = "neko";can only be used asvar neko = "neko";. If you need to use a variable asconst, for example, how about using it in a function like this:

    var neko = "neko";
    function myFunction () {
      const neko2 = neko;
      // do something

Related articles