Google Sheets power tips: Create an automatically updating spreadsheet

Monitoring fundamental information in a spreadsheet is fairly easy — it’s remembering to replace your information every day, week, or month that may be an issue. However why replace manually when you’ll be able to benefit from some easy automation instruments?Right here’s a step-by-step information to creating an mechanically updating Google Sheet that each scrapes information from the net and mechanically updates the spreadsheet. We’ll use “every day native temperature at lunchtime” on this instance, however the code will be simply tweaked to fetch and retailer different information as effectively.1. Create a Google Sheets spreadsheetHead to Google Drive or Google Sheets and create a brand new clean spreadsheet. (For those who don’t but have a Google account, you’ll be able to enroll there.) IDG/Sharon Machlis

First, begin a brand new clean spreadsheet. (Click on picture to enlarge it.)

Title the sheet something you need, and also you’re prepared to start. (The default will probably be “Untitled spreadsheet,” however you’ll be able to click on on that to alter it to one thing that may higher establish it in your listing of paperwork.)2. Discover the “XPath” for the a part of the net web page you need to scrape.Google Sheets wants an “XPath” selector to pluck only a portion of information from an internet web page. Thankfully, there’s an easy-to-use point-and-click software that helps you just do that: SelectorGadget. (There’s a less-than-2-minute video on the location demonstrating the way it works.)Set up the SelectorGadget Chrome browser extension, activate it by clicking the browser software icon, after which click on on the part of the web page you need to scrape. That part will flip inexperienced. The trick is to be sure to’re solely deciding on that. Different areas of the web page could also be inexperienced or yellow, too. If they’re, click on on those that you simply need to take away out of your choice. Within the picture under, I visited the Nationwide Climate Service web page for Needham, MA, the situation of Foundry company headquarters. (Foundry is Computerworld’s guardian firm.)This web page was simple: I clicked the temperature, and it was the one merchandise chosen. If others had proven up as inexperienced or yellow, I might have clicked these to show them off with a view to make certain my XPath was selecting solely what I want. IDG/Sharon Machlis

Choosing the temperature on the Nationwide Climate Service web page for Needham, MA. (Click on picture to enlarge it.)

Within the backside proper, a selector seems; however that .myforecast-current-lrg is a css selector. I want XPath, which is an possibility all the way in which on the backside proper. Click on on XPath, and the XPath model will present up trying one thing like this: IDG/Sharon Machlis

Grabbing the XPath selector. (Click on picture to enlarge it.)

XPath is usually extra sophisticated than CSS, however you don’t want to fret about what all of the expressions imply. The one key level is that the XPath selector will find yourself surrounded by double citation marks in your Google Sheets formulation. Sadly, the selector itself additionally has double citation marks. That can trigger issues, so in case your XPath contains double citation marks, change all of them to single quotes manually.The XPath selector I’ll use in my Google Sheet is now//*[contains(concat( ‘ ‘, @class, ‘ ‘ ), concat( ‘ ‘, ‘myforecast-current-lrg’, ‘ ‘ ))]   3. Add your formulaSpreadsheet formulation can do greater than math calculations; they will additionally extract information from internet pages. For a Google Sheet, the perform to learn a part of an internet web page is:=ImportXML(“URL”, “XPath selector”)For my Nationwide Climate Service Needham, MA web page and temperature selector, that’s=IMPORTXML(“”, “//*[contains(concat( ‘ ‘, @class, ‘ ‘ ), concat( ‘ ‘, ‘myforecast-current-lrg’, ‘ ‘ ))]”) In my spreadsheet’s first column, I’ll listing the situation I’m monitoring. It will make it simple so as to add further locations to my sheet sooner or later. I’ll put the formulation within the second column. IDG/Sharon Machlis

Inserting the formulation within the spreadsheet. (Click on picture to enlarge it.)

And I need the date and time in column C. We’ll deal with that in a second.4. Write a perform to fetch and retailer dataThe method the spreadsheet is presently arrange, the temperature gained’t be saved; it can change every time you open the sheet.To maintain historic information because the sheet’s presently designed, I’d want to repeat and paste values manually into one other cell every time I opened it. That’s not very scalable! As an alternative, let’s create a brand new perform to 1) Discover the primary empty row within the sheet and a couple of) copy the worth from cell B2 into one other empty cell for storage.To retailer information, we’ll must create a spreadsheet perform. Head to Extensions > Apps Script to create features for the spreadsheet. IDG/Sharon Machlis

Launch Apps Script to create a perform. (Click on picture to enlarge it.)

You’ll see a default perform pop up known as myFunction. IDG/Sharon Machlis

Beginning a brand new mission in Apps Script. (Click on picture to enlarge it.)

Change that perform identify to storeTemperature() (or no matter you would possibly prefer to name it), and use the next code:perform storeTemperature() {var sheet = SpreadsheetApp.getActiveSheet();var firstEmptyRow = sheet.getLastRow() + 1;// Get present temperature from cell B2var currentTempCell = sheet.getRange(2, 2);var currentTemp = currentTempCell.getValue();// Discover first empty cell in column B and add that valuevar newTempCell = sheet.getRange(“B” + firstEmptyRow);newTempCell.setValue(currentTemp)// Get the present date and time add it to identical row column Cvar currentDate = Date(); var newDateCell = sheet.getRange(“C” + firstEmptyRow);newDateCell.setValue(currentDate)// Add place identify in column A, presently arduous codedsheet.getRange(“A” + firstEmptyRow).setValue(“Needham, MA”)}The primary line of code creates a variable for no matter spreadsheet is energetic, adopted by a variable holding the variety of that sheet’s first empty row.With a purpose to learn or write information in a Sheets cell, that cell must be a variety object. Thankfully, it’s simple to show a spreadsheet’s cell location right into a cell vary object with yourSheetObject.getRange(). On this instance, I known as my sheet object sheet (you’ll be able to name it something), so the code is sheet.getRange(). getRange takes a number of codecs to establish a cell location, together with getRange(3,5) for row 3, column 5, or getRange(“B ” + 2) for typical spreadsheet cell syntax like B2.With that in thoughts, I can create a variable holding the cell vary for cell B2, which is row 2 and column 2, the place I do know my present temperature sits, utilizing sheet.getRange(2, 2). I then learn that worth with get.Worth() as you’ll be able to see within the above code.Subsequent in that code block, I discover the primary empty row in column B, the place I need to add the brand new temperature, and save that cell as a variety object. Then it’s merely a matter of setting that cell’s worth with the temperature I simply saved.The ultimate rows do one thing much like retailer the present date and time in addition to the hard-coded location.There are methods to make this code extra compact, however I’m sharing a verbose model to make it simpler to see what’s occurring. Within the final line, I tightened it up a bit by combining getRange and setValue in a single line of code, as an alternative of making a separate variable holding the cell vary. Use whichever syntax you like. IDG/Sharon Machlis

The complete Lunchtime Temps mission. (Click on picture to enlarge it.)

Save this by clicking the floppy disk icon above the perform code. You’ll in all probability need to change the default mission identify to one thing else. I named mine “Lunchtime Temps.”You’ll be able to check your script by clicking the Run button to see what occurs within the spreadsheet. If all goes effectively, you’ll have a brand new row of information with the present temperature.5. Schedule your perform to auto-runLast piece: Schedule your perform to run mechanically. To do that, click on on the clock icon on the left to open your script dashboard that exhibits your present mission’s triggers. (There gained’t be any but.) You could be requested to authorize this in your Google account first. IDG/Sharon Machlis

There gained’t be any triggers on your script till you set them up. (Click on picture to enlarge it.)

Click on on create a brand new set off, and a menu pops up. IDG/Sharon Machlis

Including a set off for the Lunchtime Temps mission. (Click on picture to enlarge it.)

If you would like your spreadsheet to replace on a selected schedule, change the occasion supply from “From spreadsheet” to “Time-driven” after which choose whether or not you need it working hourly, every day, weekly, month-to-month, or different choices.Then, voila! A self-updating spreadsheet that collects and shops information mechanically.This text was initially revealed in April 2017 and up to date in February 2023.

Copyright © 2023 IDG Communications, Inc.