Auto Run a script every X minutes - google-apps-script

I have a script where it pulls data from a google sheet that imports a range from another sheet so when I update the sheet where the range is it updates in the google sheet that is connected to a dropdown in google forms. But I have to manually re run the script in the form to update what was updated in the sheet where the ranges are. Is there a way were the script would auto run itself to get the new information from the google sheet?

You can install a time-driven trigger to execute a function with the frequency of your choice. The instructions are available here.
This is what the configuration looks like for running the function update() every 5 minutes.

Related

How can I run the googlescript code in a sheet without opening the sheet

I have a google analytics report that runs in a sheet. Using the Chrome plugin, this is set to run automatically. The results are then pulled into Data Studio on "data refresh". This works. However, I want a google script to run in the sheet.
I have written a google script routine to manipulate the Analytics data that is imported to the sheet. It runs onOpen. However, I need some automatic trigger more like "onReportRun" to trigger on the scheduled report for the Analytics -> Sheet process.
function onOpen(e) {
var paramRange= SpreadsheetApp.getActiveSheet().getRange('A2:B');
var paramValues= paramRange.getValues();
//crunch Analytics data and make two new columns and put them in the sheet. This works fine.
}
//need this:
function onGoogleSheetsAnalyticsAddOnScheduleReportRun(e) {
This runs when I open the sheet. I need it to run when the Google Sheets plugin for Analytics runs the configured report.
If you are interested as to how I ended up here, an Analytics Custom Dimension contains a list. Example: a web page has three qualities tagged against it: "health, wealth, safety" in Dimension 3. Data Studio and Analytics can't seem to split and then report on list items individually. So I export from Analytics to Sheets, run code to do this, then import the sheet to Data Studio.
How can I run the googlescript code in a sheet without opening the sheet
By simply changing onOpen to other triggers.
Google Sheets and Google Apps Script haven't triggers that runs when other code run. Perhaps your best option is to use time-driven trigger to call a "poll" function. The poll function could check certain value on your spreadsheet your could access an API to check if it should update the spreadsheet. The actual function will depend on your spreadsheet and/or the external service that you want to monitor.

Google Scripts onEdit not recognising data being edited on a sheet during a sync from a mobile device

I have a Google sheet that is updated by a mobile app created on AppSheet.
I have a column of data that I need to keep a history of so wrote a script to copy the column to a fblank column in another sheet.
function readdailyChecks() {
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Todays Checks");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("History");
// Copy from 5th column, all rows for one column
var valuesToCopy = sheetFrom.getRange("D2:D100").getValues()
//Paste to another sheet from first cell onwards
sheetTo.getRange(1,sheetTo.getLastColumn()+1,valuesToCopy.length,1).setValues(valuesToCopy);
}
I then wrote another script to do this task when the sheet was edited thinking the sync would edit the sheet data.
function onEdit(e) {
if(e);
readdailyChecks(e)
}
The idea being that the app would sync once the form was completed, update the sheet and trigger the onEdit code to do it's stuff.
The problem is that the sync changes the sheets data without editing it so the historical data is not created!
Is there an onSync code or a way that when the data changes the script can be triggered?
I work at AppSheet. When updates are made to Google sheets via the Google Sheets backend API, they do not fire the onEdit trigger. I'm not sure why exactly this is, but it is a limitation imposed by Google. So that is what you are observing.
The AppSheet documentation suggests that you try a timed trigger instead, polling for changes. https://appsheethelp.zendesk.com/hc/en-us/articles/206483017-Google-Drive
Not the greatest, but it does work. Some AppSheet users have reported success with the onChange trigger instead of the onEdit trigger. To me, this defies logic based on the documented meaning on an onChange trigger, but it appears to work for these users, so worth a shot.

How to make new sheet in google spreadsheet selected?

My script adds new sheet on a spreadsheet after some form submission. This sheet is used as a leaderboard. But to look on it I have to click on the new created tab. Is it any way to do it from script?
This picture is a screenshot after new sheet creation (Week 1 Lesson 2).
P.S.
I found that I have to reformulate the question. The problem is that I want to do it from another spreadsheet (this spreadsheet has onSubmit trigger and create and place info on other open spreadsheet (leaderboard). I check the setActiveSheet(sheet). It works only with the spreadsheet that runs the script. So I have to send some signal to leaderboard to activate sheet. But I can't understand how to do that
Straight from the developers guide
setActiveSheet(sheet)
As far as I know, there is no way to use Apps Script to change the active sheet on another open spreadsheet not running the script.
You could install a time-based trigger that ran every minute in the leaderboard sheet that checked for a new entry and then changed the active sheet as needed. This isn't instant but would take at most 60 seconds to update.

Running Google spreadsheet script from a different spreadsheet

I have several spreadsheets that collect data from multiple/different forms and I can run a script bound to that spreadsheet based on form submit.
However, what I would like to do is to have the script of the form spreadsheet access and run the script of a main spreadsheet.
The main spreadsheet collects and processes data from several different forms.
It seems that using a trigger in the main spreadsheet based onEdit only applies when a user is actually editing, not when another spreadsheet writes data to it.
Thanks for your time.
Take a look at this answer. It seems
OnEdit trigger is intended to work when an actual user edits a spreadsheet
If each of these other sheets is just adding a row to the main sheet, you can create a way that the main sheet keeps track of how many rows it knows are there. Then you set up a cron that runs every X minutes, checks if there are new rows, and does what it needs to on those.

running script on GA spreadsheet without changing previous spreadsheet data

I have some data and formulae on a Google Spreadsheet.
I want to run a new script on it which retrieves data from Google Analytics and displays it in some specific cells(mentioned in script) on the spreadsheet.
But the problem is, when I run the script, all the previous data and formulae on the spreadsheet is lost.
Is there any way by which I can keep the previous data , and at the same time run the script.??
Don't use Sheet.clear() unless you want to clear all previous content.