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.
Related
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.
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.
I wrote a small script bound to a Google spreadsheet that reads an email-address from a cell in the last row and sends an email to it.
The values are collected by a WebApp (not by Google form).
I tried all kind of triggers. The time driven triggers work, but the spreadsheet triggers don't work. I tried all of them. If I change manually some cell in the spreadsheet the onEdit trigger is working, but it doesn't fire up when the sheet was changed by another script.
Google's dev says : The onEdit() trigger runs automatically when a user changes the value of any cell in a spreadsheet. but it doesn't precise the change could be from script or not.
You'll find here a comment from HDCerberus :
I'm not at my PC to write a full response, but at a glance it could be the OnEdit trigger, which ONLY works when the sheet is edited manually, and NOT by a script. Are you expecting it to run when the sheet is edited manually or by a script?
Not every change triggers onEdit(). Please take a look on answer from topic Detect user inserting row or column in a google spreadsheet and reacting in a script - you will find registered bugs and one of them says that values written by scripts do not trigger onEdit().
I have the following spreadsheet:
-Template (protected)
-Current working sheet
-older sheet (protected)
-even older sheet (protected)
This spreadsheet is shared with a few users, and they have write permission in the "current working sheet". Once it is considered completed, I created a button in the "current working sheet" to which I assigned a script. When ran as owner, the script:
renames the "current working sheet" to "something older"
protects the "something older" sheet
duplicates the "template" sheet as a new "current working sheet"
It all works fine wen ran by owner, however when the button that triggers the script is pressed by one of the users whom I shared the document with, then step (2) fails, meaning the sheet can't be protected. I suspect this is a permissions issue but I am a complete newbie in google scripting, so I am asking for some help here. Is there any way to make sure that no matter who presses the button, the assigned script is ran as owner?
thank you
You cannot have a button which the user at the keyboard clicks invoke a script which runs as the owner. See the doc about authorization here. That said, what you can do is have the button set a value or update a record which a script on a time based trigger checks. A flow like this would work well:
User updates the working sheet
User clicks the I am done! button
Script adds a row to a hidden sheet called Things for scripts to do which indicates that as of now, the working sheet should be archived
Every hour, a script checks Things for scripts to do and updates sheets and permissions as described above
I could use some help with writing a script in my Google spreadsheet. First off, I am no programmer and a novice at best on writing code or any script.
I want to use this for a lunch list. The back story: I created Google spreadsheets to act as a digital lunch sheet. Each teacher has their own spreadsheet for their homeroom and the totals for the class populate a master lunch sheet for the head cafeteria worker for ordering. The problem is that the old totals are still present from the day before. Ideally, on the start of a new day, the specified fields on the spreadsheet will auto clear. Click here to view an example of the spreadsheet I created.
In my research I found a thread on how to create a script to do this, but as a button to click that will then clear specified ranges Click here to see the original post. The script is as followed:
function clearRange() {
//replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('C4:I12').clearContent();}
What I would like for is there to be a script so that everyday (say everyday at midnight) a specific field/ range is cleared out. I do not want columns or rows to delete because I do not want to lose student names or lunch selections.
Please, any help is greatly appreciated. Thanks in advance for your help, especially with a novice like me. I look forward to hearing from anyone!
- Jason
Follow these steps...
01. Go to https://script.google.com and then add a New Script.
ᴬᵈᵈ ⁿᵉʷ ˢᶜʳⁱᵖᵗ
02. Paste this function
function clearRange() {
// replace 'Sheet1' with your actual sheet name
// replace 'dhrhrejYOURSHETIDerhe5j54j5j' with your actual sheet ID
var sheetActive = SpreadsheetApp.openById("dhrhrejYOURSHETIDerhe5j54j5j").getSheetByName("Sheet1");
sheetActive.getRange('A:Y').clearContent();
}
ˢᵖʳᵉᵃᵈˢʰᵉᵉᵗ ᴵᴰ ᴱˣᵃᵐᵖˡᵉ
03. Goto Run & select Run function and then select clearRange.
Once you have run the script, your spreadsheet should be cleared.
Follow these steps if it works correctly...
04. Goto Edit, Select 'All your triggers'
05. In 'All your triggers' popup windows, select clearRange as the Run function.
06. Set the time as you like. (See the example image)
That script should do what you need. You just need to make a couple of modifications.
Where is says 'Sheet1' you would use 'Lunch' which is the name of your sheet in your example.
In the Script Editor, you can add a new trigger to have the script run every night.
In the Script Editor, go to the top menu "Resources » Current Project's Triggers"
Click the link "No triggers set up. Click here to add one now."
Make sure you set these values:
Run » clearRange
Events » Time-driven
Day Timer
Midnight to 1 am
That should run your script every night sometime between Midnight to 1 AM.
If you want an actual menu item that you can click on within the spreadsheet so you can run the script anytime you want, that's a bit more work. Honestly, if that's your primary function ... it's probably just easier to load up the Script Editor from the spreadsheet ("Tools » Script Editor"), and just select the clearRange function and press the play button.
If you do really want to add menu items, there's additional documentation here » https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#addMenu(String,Object). Once you've coded the new menu function, you'll have to update your triggers just like you did to run every night. But instead, you'll change the event type to From Spreadsheet and set the value to On open. That way it'll create the new menu items when the Spreadsheet is opened.