Reputation: 2649
How do I create a script that runs as soon as the user opens a spreadsheet?
Specifically I need a script that when the spreadsheet is opened asks the name and sex of the user, and depending on the gender it adds the name to a different sheet of the spreadsheet.
This is simple but I've gone through the tutorials on their webpage and couldn't do it. I know how to program but I am new to Google Apps Scripts.
Also is this something done better in Google Forms or in Google Sheets?
Upvotes: 1
Views: 1600
Reputation: 38150
To create a script that runs as soon as the user open the spreadsheet, you should create a bounded to a spreadsheet script and use a trigger:
To make the script run on open, you could use a simple trigger by using onOpen as the function name or you could use an installable trigger.
For further details see https://developers.google.com/apps-script/guides/sheets
Upvotes: 0
Reputation: 590
For script lauched at opening you need write function:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
...
};
Google Spreadsheet don't have a dialog window or something similar. But there are several ways:
Yea, I forget about inputBox() :)
Upvotes: 0
Reputation: 46794
Nothing too hard to get that :
read this doc on how to build an alert in a spreadsheet and make it show up using an installable trigger onOpen
all you have to do is put these together.
Upvotes: 1
Reputation: 45710
How do I create a script that runs as soon as the user opens a spreadsheet?
Specifically I need a script that when the spreadsheet is opened asks the name and sex of the user, and depending on the gender it adds the name to a different sheet of the spreadsheet.
The onOpen() should call a function that uses Browser.inputBox() to get the user's input, then writes it to the sheet via Range.setValues().
Note that the user will need to have edit privileges for this to work.
Also is this something done better in google forms or in google spreadsheets?
If you want the UI to show up in a spreadsheet, then the script must be contained in a spreadsheet.
Alternatively, if you don't want the user to see the spreadsheet, you could use the Forms service to collect their input, with no need for any programming.
Upvotes: 4