P
P
PankinGS2020-04-27 22:39:17
Google Apps Script
PankinGS, 2020-04-27 22:39:17

Hierarchy of files in a project?

On docs.google.com/spreadsheets/ I created a table with my data, then switched to the script editor. After the transition, a project was created on script.google.com tied to this table.
There is a file created

code.gs
by default, but it can be renamed.

Explain the question about the hierarchy of files, which one is launched first.
The documentation says that when a table is opened, onOpen() is run first.
And if I rename the file created by default and create another one with the name:
code.gs
?

Rationale: there is a file
code.gs
and the onOpen() function in it.
But when I created the print.gs file in this project and created my own function in it, the first function from this file began to run first when the table was opened (not onOpen).
Why and where to read?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2020-04-28
@oshliaer

It depends on the runtime version V8, STABLE, DEPRECATED_ES5.
For V8the order of the files, it definitely does not matter, and not even all files are loaded for execution. Therefore, it is better to avoid this convention.
In STABLE, DEPRECATED_ES5the order usually depends on the alphabetical arrangement of the files, but this is not guaranteed.
What's going on with you.
One project has two files код.gsand код2.gs. Both have a function onOpen. When loading the program text into the execution environment, one function will overwrite the other, and in fact only one function will be executed, because there is no other at all (it is overwritten). Which function will be overwritten depends on the environment (see above).
What to do.
One solution could be to create one onOpen function and call two others from it

function onOpen(e){
  onOpen1(e);
  onOpen2(e);
}

function onOpen1(e){
  ...
}

function onOpen2(e){
  ...
}

Or you can create another project in the same container [Table] (see the current project menu).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question