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.
Related
I'm running an online booking sheet for my local squash club (dragging them into this century!) and need to find an easy way to automate navigation to the current date when #1 opening the spreadsheet, an #2 switching sheets while the spreadsheet is open. Without the feature, people will have to endlessly scroll through the season to find today's date.
I've run a formula in column A that will place an * on the row that =TODAY() and would appreciate a bit of advice from people with script writing skills as to whether it would be possible to go to that cell on start up, and when we switch between sheets (we run 2 courts, on separate sheets).
As always, help is very much appreciated, and hopefully there is something that can be introduced that can help.
[edit] I suppose the simplest way, looking back at it now, would be the following I just need to learn how to code it:
OnOpen - Set the active sheet as "COURT 1"
Set the active range for the function as A:A
Go to the last modified cell in that range (as it's always going to the be the cell that I want).
The script should then repeat the process for "COURT 2" and then return to "COURT 1".
Copy of the spreadsheet can be found here:
https://docs.google.com/spreadsheets/d/1NBom_qB9AM_LG2lgrGjNRDbxbSGysVj8H6AXqdgKWD8/edit?usp=sharing
Thanks :)
Mark
Some key concepts that might help. I highly advise that you learn how to script so you don't go asking for code each time you want to modify something.
To run a script upon opening the sheet, you can make use of onOpen() trigger. On the same note, you need to be aware of this trigger's Restrictions.
To operate on another sheet within the same SpreadSheet, use setActiveSheet(sheet). When the sheet becomes active, you can now do operations on it. Check the SpreadsheetApp for more docs info.
So I have a Google sheet where, when a user enters a number of an item, it will output a description and price. I actually have 50 sheets (one for each state in the US) that are all almost exactly the same, but put out slightly different prices because state taxes vary from state to state.
I used onEdit() to have my sheet work and it was working fine until I changed where the source for information came from. Originally in my sheet, I had another page with all the item information so that a simple Vlookup could do most of the work except calculate the item's price (this is what my code was doing, using the info page that was in the sheet to calculate a price).
However, when an edit needs to be made to an item, I want to make it so that we only have to update one "master" sheet, and make a call by openByUrl(...) instead of going to all 50 sheets and copy pasting the information. I tried implementing this in a sheet, and now it doesn't work when I edit, but it does work when I manually go into script editor and press run. What gives?
EDIT: Here's the code requested.
function onEdit(d) {
itemPriceSetup();
}
// Runs the actual program.
function itemPriceSetup() {
// Grabs and stores the sheet where a customer places an item number and where the code will output the price to.
var orderSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Item Sale Doc");
var orderSheetArray = orderSheet.getSheetValues(1, 1, 34, 8);
// Grabs and stores the sheet that has the information on the item.
//***var infoSheet = SpreadsheetApp.openByUrl('link to info');
var infoSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet with info");
var infoSheetArray = infoSheet.getSheetValues(1, 1, infoSheet.getLastRow(), 10);
So the code with the three asterisks is what I want to use, but causes my the program to not work - that is, it onEdit() won't run (I have it commented out so the code will run - the line below it is the one I'm trying to replace). If I were to go through the debugger with the line un-commented, it actually works.
So I figured it out, but it's a bit strange. I didn't realize triggers for a script are found under resources, so I made put one straight in my script (I guess?). Either way, use the link and go to "Managing triggers manually" to read on how to do it.
https://developers.google.com/apps-script/guides/triggers/installable
I had the same problem onEdit(e) wasn't working. After trying out many different things disabling the new Apps Script V8 engine worked for me. To disable go to Script-menu Select Run and click disable the engine
Dissabling new Apps Script V8 engine
I hope this answer will helps many. I appreciate all of the help that I've received on StackOverflow.Thank you
I have the following issue:
'I have a Google Sheet that contains copy information that is used in an app, when the app is modified by the copy person, I would like tor receive an email so I know to update the strings used inside of the app.' There are other non-automatic ways to achieve this, but the copy person knows google docs really well, and sometimes doesn't remember to notify me of changes, or 'insert other reason here'.
At any rate, I've created the function that generates a set of strings I can paste into Xcode and run the app. The only manual part is now running the function and getting the output into Xcode. The output is stored in a separate sheet of the same document. (Newest copy at the top).
The technical question / point of this post, how do I take this to the next step of emailing me so I can make the changes to the Xcode strings file?'
I am open to suggestions / other thoughts as I'm trying to iterate on this and I feel like once I get this script to run whenever the copy person updates the sheet, I will be good for some time.
Here's where I'm at:
I tried searching through Google's documentation, SO, and other sites, and found the following:
How to access revision history of a spreadsheet using GAS?
https://code.google.com/p/google-apps-script-issues/issues/detail?id=394
stackoverflow_com/questions/10584528/documentlist-api-and-gas-how-to-marry-them
sites_google_com/site/scriptsexamples/new-connectors-to-google-services/driveservice
productforums_google_com/forum/#!topic/docs/zty8X8Pkwbs
(the above are not links as my reputation is insufficient to post more than 2 links at this time).
All of these sources suggest there is a way to potentially solve this problem, but it's quite a task for something as simple as seeing 'is the last editor someone who's changes should cause this script function to run'.
My thoughts were to have my function run whenever the sheet is edited, and so long as the last modifier is the copy person, then run, otherwise, do nothing.
I may also be missing a small detail/quirk that could prevent what I want to achieve from even being possible. Hoping to find some insight / answers through this post.
A quick solution would be to have your 'copy person' create a bound script in the spreadsheet with something like:
function myFunction() {
var user = Session.getActiveUser().getEmail();
if(user =='user-email'){
MailApp.sendEmail('destination-email', 'subject', 'test trigger: ' + user);
}
}
Run the function just for testing and to grant permissions.
Then he will have to create a Trigger by going to the menu "Resources -> All your triggers".
Then click on "No triggers set up. Click here to add one now."
Select the name of the function.
Instead of time-driven, select "From Spreadsheet"
Then select the event "On edit"
and save the trigger.
Now, the 'copy person' should modify the spreadsheet just to test that the email is sent.
Keep in mind that with this approach, every single modification made by the 'copy person' will trigger the event and you will receive a new email.
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.
I am new to google apps scripts and I am following the tutorial at https://developers.google.com/apps-script/articles/sites_tutorial and when I run the myContact function from step 1, no contact info is populated into my spreadsheet.
I have a group created called AZ_Pilot, and I have the myContact function set up as follows:
function myContact() {
var contacts = ContactsApp.findContactGroup("AZ_Pilot").getContacts();
SpreadsheetApp.getActiveRange().setValue(contacts[0].getPrimaryEmail());
}
The script runs with no errors, but nothing is updated within the spreadsheet. It's a bit embarrassing to be stumped at step 1, but here we are.
You are using getActiveRange() which is the range which is currently under selection in the spreadsheet. By default, it is the first cell in the spreadsheet. Maybe you have a different cell selected and therefore, the email might be written to a different cell. Try keeping the first cell in focus and running the function again.
Also, you haven't mentioned whether or not there are any contacts in the group you created :)
I was adding some additional logging information, and it just started working correctly with no change to the functional code. Not sure what happened there.