For this program you will select from a list of planets and then learn to 'scrape' a webpage to find the mass, diameter and an image of the planet. Surface gravity will then be calculated for the planetary body.
INPUTS:
The user will select a planet or the moon from a dropdown list.
OUTPUTS:
The diameter of the planet in miles will be printed.
The mass in kilograms will be printed.
The surface gravity will be calculated and printed.
A picture of the planet will be displayed.
CRITERIA & CONSTRAINTS:
Your program should have an impressive interface. (colors and graphics)
A dropdown list of planets as well as the moon should be offered.
The mass, diameter and planet image will be 'scraped' from https://nssdc.gsfc.nasa.gov/planetary/factsheet/
Create a function called newton( )
Create a variable called sheet to get the active sheet.
Create a list variable called planetary_bodies which includes the following: Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune and Pluto. For purposes you will see later the list needs to be in that order.
To create a dropdown menu of planets we need to create a Validation variable called planets into which we will add the planetary_bodies variable.
This can be done like: var planets = SpreadsheetApp.newDataValidation().requireValueInList(planetary_bodies, true).build();
Create a variable called set_planets and assign the validation to it as follows: var set_planets = sheet.getRange("H7").setDataValidation(planets);
Create a variable called planet and get it from what the user selected.
Determine the index number of planet in the planetary_bodies list as follows: var id = planetary_bodies.indexOf(planet);
In this program, we will be learning how to "scrape" data from the internet. Navigate to https://nssdc.gsfc.nasa.gov/planetary/factsheet/ and check out the table of planetary data on the NASA website. There is a built in function in Google Sheets called ImportHTML which is very handy. It can be used to bring in data from websites that contain 'lists' or 'tables' such as the one on the NASA website. To try it out, open up a blank google sheet and in cell A1 type in the following code: =ImportHtml("https://nssdc.gsfc.nasa.gov/planetary/factsheet/", "table",1)
The ImportHtml( ) function took 3 parameters: the URL of the webpage to be scraped, the type "table" or "list", and the number of the table or list on the page if there are more than one.
Let's say we just want one piece of data instead of the entire table. (ex. Escape Velocity of Jupiter) We can use the =Index( ) function around the ImportHtml( ) function as follows: =Index(ImportHtml("https://nssdc.gsfc.nasa.gov/planetary/factsheet/", "table",1),6,7)
The Index( ) function took 3 parameters: the ImportHtml( ) function, the row number, and the column number. This might seem a little backwards but you count down the rows and then over the columns. The Escape Velocity of Jupiter is 6 rows down and 7 columns over.
Since the id number of Jupiter in the planetary_bodies list is 5 but the column number for Jupiter in the table is 7 we will need to go back up to our index code and add 2, so we can use the planet's id number for the column reference in the Nasa table: var id = planetary_bodies.indexOf(planet)+2;
Now we'll make a variable called mass as follows: var mass = '=INDEX(ImportHtml("https://nssdc.gsfc.nasa.gov/planetary/factsheet/", "table",1),2,'+id+')'; Notice the row is 2 (the mass row) and the column is the id of the planet.
Now make a similar variable called diameter but use row 3.
Print both the mass and diameter variables (which are actually sheet functions) to the spreadsheet. I used cells H8 and H9.
Now get the results back from cells H8 and H9 and reassign them back to the mass and the diameter variables. Create a variable called mass_kg and set it equal to the mass times 1E24. Create a variable called dia_miles and set it equal to the diameter converted to miles. Print both the mass_kg and dia_miles variables back to the spreadsheet in cells H8 and H9.
Create a variable called gravity and calculate it from the mass and diameter. Print the gravity to the spreadsheet in cell H10.
We will now learn to scrape images off of the internet also. If you click on one of the planet headers in the table on the NASA website it will take you to a page about that planet. Right-click on the image and "copy link address". The address I found for Jupiter looks like this: https://nssdc.gsfc.nasa.gov/planetary/image/jupiter.jpg
Go back to your blank spreadsheet and make a cell on the sheet to be about an inch wide and an inch tall. Into the cell we will type the =image( ) function as seen below:
=image("https://nssdc.gsfc.nasa.gov/planetary/image/jupiter.jpg",1) The image( ) function took two parameters: the URL where the image is and then a number 1-4. A (1) means the image will be scaled to fit the cell. A (2) means the image will be stretched to fit the cell. A (3) means the image will retain it's original size and a (4) means you can make it a custom size by adding in a width and height. We will just go with the (1). See below for an example of the image( ) function using the Google logo.
If you check several URLs of the planetary images you will notice that the only difference is the name of the planetary body right before the .jpg. This means we can dynamically create the URL with whatever planet the user selects. However in our dropdown list the planets all started with a capital letter whereas the planets in the link are all lowercase. To fix this we will turn the planet variable into all lowercase like this: planet=planet.toLowerCase( );
Now we can create a variable called getImage and assign it as follows: var getImage = '=image("https://nssdc.gsfc.nasa.gov/planetary/image/'+planet+'.jpg",1)'; I printed this to cell G12.
The pluto link showed nh_pluto instead of just pluto so you might have to use an if-statement to make sure Pluto shows up ok.
Create a button for the user to click to run the program.