Is there anyway to fire an onChange trigger on a Google spreadsheet when the change was made by an App Script?
For context, I've got an app that pulls metrics from Google Analytics for individual blog posts. My challenge is when there are more than 50 blog posts (each on its own row) in a spreadsheet, or if the data is slow coming back from Google Analytics API for any reason, the application exceeds the time limit. I'd like to configure the sheet so the app runs once for each blog post, and then once the data is changed on the spreadsheet, the app is triggered to run again.
Thanks!
its not possible to trigger the onChange/onEdit etc from script.
however you can do this:
use an extra column to store the last processed date per row.
from your time trigger check if the last modified date of the spreadsheet (from DriveApp) is after the date stored per row.
this allows the trigger to run even once a minute while consuming little quota on the simple checks.
Related
I am looking for technology suggestions. Or if this can be done in native google sheets (note the site I am looking to access is behind a username and password).
I have a google sheet that looks like this
birth date
link
data_element_from_website
12/31
https://something.com/3920230
1/31
https://something.com/1920238
lets say on https://something.com/3920230 there is a HTML element 123
Twice a day I want to be able to refresh the data, this could be done by going into the spreadsheet and clicking/doing something.
Can this be done?
What if https://something.com/3920230 is behind a login (authentication). Note: I could be logged in to the website in a different tab... I don't think that would make a difference though...
Assuming you have a script that you would like to run twice a day, you can use Apps Script Time-driven triggers:
A time-driven trigger (also called a clock trigger) is similar to a cron job in Unix. Time-driven triggers let scripts execute at a particular time or on a recurring interval, as frequently as every minute or as infrequently as once per month. (Note that an add-on can use a time-driven trigger once per hour at most.)
If it's just some formulae, you can change the recalculation settings to be either On change, On change and every minute or On change and every hour under the File > Spreadsheet settings menu item and clicking on the Calculation tab.
If you want to get data from public site(no login) you can use ImportXML. You can google a lot about it, for example
https://answerbun.com/personal-finance-money/get-revenue-details-in-google-sheets-using-google-finance/
If the page you want can only be accessed using login, this becomes a rather complicated task, especially using only apps script libraries (not much scraping lib support here)
We're building a platform that fetches data from the AdWords accounts under our AdWords Manager account and saves the data onto BigQuery to be viewed online via nice charts and tables.
As you all know, Adwords numbers need a few days to stabilize. So, every day at 12am we need to refetch all the data for the past 2 weeks and overwrite the data we already have saved for these 2 weeks (we're basically updating the numbers we have).
Our AdWords manager account has 150+ accounts under it. So, when we run the script that fetches the data for all these accounts for the past 2 weeks, understandably, the script times out because it needs more than 65 minutes to be done with the task.
When we looked online for solutions, the only thing we could find was using the "executeInParallel" function provided by the Adwords Script. This should allow us to run a function on several accounts at the same time. Unfortunately, the "executeInParallel" function cannot be called on more than 50 accounts. Since we have 150+ accounts, we cannot call the "executeInParallel" function on them.
We tried splitting the accounts into groups of 50 and calling the "executeInParallel" function on these groups. However, the "executeInParallel" function cannot be called more than once in a single script. This means that we're not able to use this solution.
The only other solution we can think of is to create a script for each one of the 14 days and have each script would fetch the data of all the accounts for a specific day. So, Script1 would fetch the data for today -1. Script2 would fetch the data for today -2 ... Script14 would fetch the data for today -14.
Does anyone else have another solution that we can use?
I never used the adwords apps script service but I see it works with iterators so in apps script we are used to use that with Drive service. What I would do, based on this example script :
var accountSelector = AdsManagerApp
.accounts()
.withCondition("Impressions > 100")
.forDateRange("LAST_MONTH")
.orderBy("Clicks DESC");
var accountIterator = accountSelector.get();
while (accountIterator.hasNext()) {
var account = accountIterator.next();
}
What you can do :
You create a json object where you will store the account id treated.
You store this json in a script property, you have to use json.stringify in order to save the json as text in theproperty.
You use trigger to restart program before it goes over time limit.
Idea is :
You start the program
you initiate the json by getting the property, if empty create blank json
you initiate account selector
you start the while(), inside you test first before to get data if the account is already treated, i.e. have an entry in the json.
If not you get the data and once done you add an entry in the json
I recommend to do that for 10 account for example and not waiting time overdue
When 10 accounts are treated you go out the while (break;) to push the json in the script property
Last, you create a trigger to rerun the function after 90 seconds.
ScriptApp.newTrigger("myFunction")
.timeBased()
.after(90 * 1000)
.create();
Don't forget to delete triggers before to create a new one
Don't forget when you finish iterator to stop everything and reset property for newt run.
Stéphane
I have created a Google AppScript project for Google Sheets that fetches data from an API and adds it to the spreadsheet. This is already published on the Google Sheet addon store.
I want to run a trigger as frequently as possible to cache the frequently accessed data.
One way I've found how to run something as often as I want (At least every 5 minutes) is through Current project's triggers option in the Google AppScript project page (where you edit the code). I could create a function that hits an endpoint to get the most frequently access data, cache them to the script cache (CacheService.getScriptCache) and run it every 5 minutes using a trigger.
So, is the Script Cache that my code accesses when run from the current project's triggers, is it the the same as the users of the published addon ?
Because if it's not, then the caching obviously won't benefit the users that has installed the addon from the addon store.
I would like to synchronize a google spreadsheet with a map so that I don't have to upload everything everyday.
I found that it's possible to synch a google form to google map using Google Fusion.
See, YouTube: Syncing Google Forms with Google Fusion Tables for Crowdsourced Maps.
But I couldn't replicate the process to my situation (I guess it's maybe because the spreadsheet content is not originated from a google form and maybe the script take that into account)
I don't know much about coding scripts but automating this process would be a blast for me!!
I hope someone will be able to help me out on this
thanks a lot and have a good day
The only thing to account for this situation is the difference of form submit. The guy in the video sets up two triggers: one for onFormSubmit, and one hourly trigger for syncing whenever any manual changes are made.
I haven't looked directly over the code, but all you should have to do is modify the onFormSubmit code and trigger. Change the code to look for and update the fusion table with any new rows from your spreadsheet. And then change the trigger to your desired need, timer would probably be the best option. So every hour, or day, or run it manually after your done adding rows.
Now, if you were to edit the rows of data after they've already been updated, the hourly syncing will take care of those changes.
I could imagine that the hourly sync method could be changed in such a manner to look for rows that need to be added, could be as simple as calling the submit function.
I had the same problem but i could solve it.
A time trigger is not needed if you set the sync function at the end of the function OnFormSubmit (so "sync();" under "insertRowId(rowId, row);" Syncing takes place after each sending of the form automatically.
For larger forms I found out that you should not make a special column Location in the Fusion table. The address column should marked as Location in Fusion table. In the script properties of the spreadsheet give the addressColumn the value of the column title of the address column and the third property keeps unchanged ("latlng">Location. What happens is that the value of the adres is overwritten by "latlng". So if you have trouble to loose the original addresses, add a new column, copy by apps script the same address (that piece of script direct at the beginning of the function OnFormSubmit) and (after syncing) give the addressColumn the value of the column title and in the Fusion table marks the original addess column as Text and the new column as Location.
I have a function (in a Google Apps Script), not called onEdit(), but something else and this has been set up to be called from any on edit events via in the Installable onEdit on a Google Spreadsheet.
The spreadsheet is around stocks and monitors a portfolio and the purpose of the function is to send an email as soon as a stop loss is hit on a stock, i.e. the price of a stock falls to a certain price. The price of the stock is retrieved and updated via the Google Finance API in a column in the spreadsheet.
Now, when running the function from the script or locally editing the spreadsheet, the function is called successfully. However, I am finding that as the stock price is updating automatically throughout the day on Google Drive and falling to the stop loss price, the on edit function is not being called. I read somewhere about it not being called by an "anonymous" user? I know it's not being called as I have an "email counter" column, which decreases every time the function is called, and once at 0 no more mails will be sent to avoid spamming. This not decreasing via the Google Finance API automatic price updates.
Can someone advise what is causing this and if this there is any workaround?
A list of things-that-don't-trigger-onEdit, along with their issue tracking ids, were provided in a previous answer.
Content of a spreadsheet changed by scripts does not trigger onEdit.
A work-around might be to use a time-based trigger, and in the call-back scan for and react ti changes. (Could the updater set a trigger to fire in the near future, perhaps?) Challenges for this will be around balancing responsiveness vs trigger limits.