I have recently taken up on writing in Google Apps Script.
Currently I have created a function that is "attached" or "linked to" a Google Form. I did this by going to the editing screen for my form and accessing the script editor from there.
The code creates a new spreadsheet and links the form to that spreadsheet. The reason for this is that every week I would like to create a new spreadsheet for the form to submit to.
The issue I am running into is that when I try to manually make a trigger I do not see the option for it. The steps I've taken are as follows Resources -> Current Project's Triggers -> Click to add a new one.
The only options I see are "on form submit" and "on open"
Is there a way to programmatically add a time based trigger?
There is a way for creating the time based trigger. When you want to create a trigger at Feb. 1, 2017, 18:00 using GAS, you can do that by following script. 'test' is a function you want to execute.
var tr = new Date(2017,2-1,1,18,00);
ScriptApp.newTrigger('test').timeBased().at(tr).create();
Reference URL is https://developers.google.com/apps-script/reference/script/script-app#newTrigger(String).
Related
I have a Google Sheets spreadsheet that's been working for months, and has no scripts associated with it. Recently I decided I'd like to log the date of change to a row, and that the onEdit() function could do that for me.
From the spreadsheet display, I selected Tools / Script Editor, which popped up an empty script with a myFunction() function.
I modified myFunction() to:
function onEdit(e) {
Logger.log("Something was edited.");
}
Then pressed Save and accepted the default progject name (Untitled project). I also tested this (Run) and it created a log entry.
Now when I edit any value in the sheet or add a new row, I would expect a log entry. But none appears.
Have I missed a step? What must one do in order to get a valid onEdit() to run?
Your logs will be available at View>Stackdriver logging. View>Logs will only show logs executed in the current session (by clicking "Run"). Using console class is preferred.
Disclaimer: I don't often work with the Script Editor in Google Sheets, nor Google Sheets itself, too often. There may be a better way to accomplish this.
In my experience, I've found you also have to explicitly add a trigger to your Script Editor project to get the events to fire correctly.
From your sheet, open your Apps Script project by selecting the Tools > Script Editor menu option.
From the resulting Apps Script project screen, select the Edit > Current Project's Triggers menu option.
From the resulting Triggers view, click the + Add Trigger button (appeared in the lower-right of the interface presented to me; your mileage may vary).
In the resulting modal, select the following options:
"Choose which function to run" - select the onEdit method to execute the method you've written.
"Choose which deployment should run" - I've selected the "Head" option, but I'd imagine your mileage will vary wildly if you've elected to use the more advanced deployment features here.
"Select event source" - set this to "From spreadsheet".
"Select event type" - set this to "On edit".
"Failure notification settings" - set this to whatever setting you'd like.
Finally, click the "Save" button.
Once your trigger is set up, the spreadsheet should now be running your method whenever an edit to the spreadsheet is made.
Answer:
Make sure you run the onEdit(e) function from the Apps Script UI using the play button (►) and authorise the script to run as you.
My Google Sheets script programmatically creates a form, and then attempts to change the name of the Form Responses tab that was added to the spreadsheet. The form is created fine, but I can't seem to access the responses tab, at least from the same function. It appears that the Sheets object is not getting updated with the new tab.
However, if I run a separate function later that tries to access the responses tab, this works fine.
Any suggestions for how to get the Spreadsheet to update and reflect the new set of Sheets? I was hoping I could use something like a setTimeout but doesn't sound like that is available in Google script. A time-based trigger wouldn't work because I want this function to be able to happen quickly (not after a minute).
I want to have a Goggle Apps Script be run when a new Calendar Event is added to a Google Calendar. How can I setup the trigger? I know what I want to script (change color for an event based on the text in the Title). I just don't see where to setup the trigger to run my script.
You can use the new Google Calendar Event Triggers. You can create the code with a script similar to the following to trigger your function:
ScriptApp
.newTrigger('findNewEvents')
.forUserCalendar(Session.getActiveUser().getEmail())
.onEventUpdated()
.create()
Then you can loop through the events in your calendar to find any events recently created and pass them to your function.
Go to https://script.google.com/home/triggers (also available in your script editor, go to Edit -> All your triggers).
Create or edit the trigger for your script.
For event source use "From calendar", put "Calendar updated" to calendar details (see the image below) and specify the calendar owner email.
This feature is not supported yet as of now. You can see that no resolution has been provided in this issue tracker. You will have to rely on your own workaround implementation or open a new feature request.
I am trying to send a mail with attaching a file available in my google drive using following google scripts
var file=DriveApp.getFileById('1qZVK0UZ1jLbDdj10FXZqeAVEodvxEy2Bs');
MailApp.sendEmail('xyz#gmail.com','subject','body',{attachments[file.getAs(MimeType.PDF)]});
when I run above script manually it runs fine, email gets sent. But when I run it using form Submission trigger it gives error "You do not have permission to call openById"
steps that will reproduce the problem?
create a google form.
write above scripts on script editor and add a trigger with submission of the form.
fill and submit the form then you will get above error into mail from 'apps-scripts-notifications#google.com'
Please help.
I ran into this same issue, but felt it was a little silly to have to use the FormResponses Sheet to get the submission event (I mean there's a trigger on the Form Apps Scripts for a reason, right?).
All I had to do was to Remove and Re-add the trigger for From form --> On form submit. When I added it, challenged me for OAuth permissions to view and use my google drive.
Immediately after this, I was able to run on a Forms Apps Script Submission trigger again.
I found this when creating the script on the Form itself.
To solve the problem first have you form responses sent to a spreadsheet.
From you form go to Tools > Script editor... copy you script and delete any triggers you have set up there, you won't be needing this script anymore.
Now click on View responses button in your form to open the spreadsheet.
Go to Tools > Script editor... (for the spreadsheet).
You should start with a blank project.
Delete anything in there and paste in your script.
Save and name you project.
Now set up your triggers as normal. You will find the third drop down list of the new trigger has the option On form submit.
Once your script is authorised any time the form is submitted the event will trigger and will have access to the DriveApp.
Once you've tested and got it working I would go back to the form and delete the script project in there. I do that to keep things tidy and save confusion if I come to alter the script later and have forgotten which one is actually live.
I don't know if there is a limitation with Google Forms that limits their access to Drive or something.
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?
How do I create a script that runs as soon as the user opens a spreadsheet?
An onOpen() trigger function.
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.
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.
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:
You can use show method to show your html or UiApp input dialogbox: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#show(Object)
you can use one sheet for input "sex" and hide others sheets. After click button you can show all sheets. Here is SpreadSheet API: https://developers.google.com/apps-script/reference/spreadsheet/
you can use information from profile logged person and read his/her sex :)
you can use Form for input "sex" and switch context (by script) to spreadsheet but this is more complex solution. Here is Form API: https://developers.google.com/apps-script/reference/forms/
Yea, I forget about inputBox() :)
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:
Create a new or open an existing spreadsheet.
Click on Tools > Script editor...
Click the close button on the welcome dialog.
Start writing your script
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