- open
sample-spreadsheet.csv
in google sheets - in the toolbar, go to Tools > Script Editor
- copy and paste
Code.gs
into 'Code.gs' - hit 'save' (you will have to enter a project name)
- go back to your spreadsheet and refresh the page
- in the toolbar, click Custom functions > Look up fragrance notes (you will need to authorize the script)
- it should populate all empty cells in the 'Fragrance notes' column with the fragrance notes listed on fragrantica.com
- run whenever you add new rows to your sheet!
-
-
Save aknik/891386260d7ddbae45e8b9f34f8bf7ff to your computer and use it in GitHub Desktop.
google apps script to populate a google sheet w/ fragrance notes from fragrantica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Custom functions') | |
.addItem('Look up fragrance notes', 'getFragranceNotes') | |
.addToUi(); | |
} | |
function getFragranceNotes() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var allSheets = ss.getSheets(); | |
var baseUrl = 'https://duckduckgo.com/?q=!ducky+'; | |
var siteParam = '+site%3Afragrantica.com/perfume' | |
for (var sheet in allSheets){ | |
var currentSheet = allSheets[sheet] | |
var selection = currentSheet.getDataRange(); | |
var rows = selection.getNumRows() + 1; | |
var perfumerColumn = 1; // column A | |
var titleColumn = 2; // column B | |
var fragranceNotesColumn = 3; // column C | |
for (var row = 2; row < rows; row++) { | |
var targetCell = selection.getCell(row,fragranceNotesColumn); | |
if (targetCell.isBlank()) { | |
var perfumer = selection.getCell(row,perfumerColumn).getValue() | |
var title = selection.getCell(row,titleColumn).getValue() | |
var searchQuery = (perfumer + ' ' + title).replace(/ /g, '+') | |
var searchUrl = baseUrl + searchQuery + siteParam | |
var fragranceNotes = '' | |
// https://stackoverflow.com/questions/19455158/what-is-the-best-way-to-parse-html-in-google-apps-script | |
// https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html | |
var page = UrlFetchApp.fetch(searchUrl).getContentText(); | |
var parsedPage = Xml.parse(page, true) // deprecated, can deal w/ malformed html | |
var xmlString = parsedPage.html.body.toXmlString(); | |
var parsedXml = XmlService.parse(xmlString); | |
var html = parsedXml.getRootElement(); | |
var targetDiv = getElementById(html, 'prettyPhotoGallery'); | |
var spans = getElementsByTagName(targetDiv, 'span'); | |
for (var i in spans) { | |
fragranceNotes += XmlService.getRawFormat().format(spans[i])+', '; | |
} | |
fragranceNotes = fragranceNotes.replace(/<.*?>/g, '') | |
.replace(/main accords, |Pictures, |Videos, | , /g, '') | |
.replace(/^[,\s]+|[,\s]+$/g, ''); | |
targetCell.setValue(fragranceNotes); | |
} | |
} | |
} | |
} | |
function getElementById(element, idToFind) { | |
var descendants = element.getDescendants(); | |
for(i in descendants) { | |
var elt = descendants[i].asElement(); | |
if( elt !=null) { | |
var id = elt.getAttribute('id'); | |
if( id !=null && id.getValue()== idToFind) return elt; | |
} | |
} | |
} | |
function getElementsByTagName(element, tagName) { | |
var data = []; | |
var descendants = element.getDescendants(); | |
for(i in descendants) { | |
var elt = descendants[i].asElement(); | |
if( elt !=null && elt.getName()== tagName) data.push(elt); | |
} | |
return data; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Brand | Title | Notes | |
---|---|---|---|
Aroma M | Geisha Vanilla Hinoki | woody, aromatic, fresh spicy, citrus, floral | |
CB I Hate Perfume | To See A Flower | earthy, green, yellow floral, floral, warm spicy, woody | |
DS and Durga | White Peacock Lily | white floral, aquatic, floral, sweet, musky | |
Heeley | Iris de Nuit | powdery, musky, floral, woody, amber | |
Monsillage | Pays Dogon | woody, fresh spicy, earthy, green, aromatic | |
William Eadon | No. 12 | warm spicy, citrus, aromatic, fresh spicy, musky, floral |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Brand | Title | Fragrance notes | |
---|---|---|---|
Aroma M | Geisha Vanilla Hinoki | ||
CB I Hate Perfume | To See A Flower | ||
DS and Durga | White Peacock Lily | ||
Heeley | Iris de Nuit | ||
Monsillage | Pays Dogon | ||
William Eadon | No. 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment