Tải bản đầy đủ - 0 (trang)
Hack 26. Automatically Complete Lists of Related Items

Hack 26. Automatically Complete Lists of Related Items

Tải bản đầy đủ - 0trang

loopfortherepeatedcells.

HackingtheHack

Youcanevengrowarbitrarylistswithadifferentfeature:

Magic-Fill.Openanewspreadsheetandtypethefollowing

superheronamesintothecells:

Batman

Superman

WonderWoman



Expandingthislistasusualwilljustrepeatthelist.Buthere'sa

trick:insteadofjustdraggingthehandleinthebottom-right

cornerdown,asshowninFigure3-22,thistimeholddownthe

Controlkey(OptionontheMac)whiledoingso.Thelistthat

appearswillnowshowsomethinglikethefollowing:

Batman

Superman

WonderWoman

batman

wonderwoman

superman

greenlantern

spiderman

jla

aquaman

theflash





ContinuingalistwiththeMagic-Fillfeature



TheFlashisasuperhero;soisSpider-Man,andtheJLAisthe

JusticeLeagueofAmerica,asuperherogroup.Butholy

batarang,howonearthwasGoogleabletotellthatyouare

lookingfornewsuperheronames?Thisiscertainlymorethan

youmightbeusedtofromExcel.Theanswertothisfeaturelies

inamemberoftheGoogleLabsservicearraycalledGoogle

Sets.Givethetoolatrybyvisitinghttp://labs.google.com/sets

andenterthenamesGeorgeClooney,BradPitt,andHarrison

Fordintothefirstthreeboxes,aspicturedinFigure3-23.Press

theLargeSetbutton,andyouwillseealistlikethisone:

bradpitt

georgeclooney

harrisonford

tomcruise

angelinajolie

johnnydepp

juliaroberts

mattdamon

brucewillis

jenniferlopez

britneyspears

...



NotallamongtheresultsreturnedaremaleHollywoodactors,

astheoriginalnamesmighthavesuggested,butGoogleseems

tohaveunderstoodyourseedlistcontainedU.S.celebrity

names.Youcantrythiswithanyotherseedlist,too(though

Googleincertaincaseswon'tbeabletofindgoodresultsfor

you).Herearesomeexampleliststhatyoucantrygrowingin

GoogleSpreadsheetsorGoogleSets:

ford,hummer,bmw,...

martinscorsese,stanleykubrick,toddsolondz,...

bigmac,hamburger,cheeseburger,...

theoryofrelativity,quantumtheory,...





GoogleSets,thebasisforMagic-Fill



Hack27.ImportDatafromWebSites

Screenscrapebitsandpiecesfromanywebpageforautomated

inclusioninyourspreadsheets.

GoogleSpreadsheetsoffersyouasetofdefaultfunctionsto

lookupfinancialdata,ortriviafromtheirQ&Adatabase[Hack

#19].Butyoucangobeyondthisfeatureandaccesscustom

datafromanywebpage,suchasalistoflinksfromablog,ora

listoftitlesorISBNnumbersfromabookseller'swebsite.

Therearefourkeyfunctionsthatcanimportexternalwebdata:

importXml,importData,importHtml,andgoogleReader.The

mostflexibleoftheseisimportXml,asitturnsanykindofHTML

intostructureddata.

Supposethatyouwanttoautomaticallyaddalistofbooktitles

onthesubjectofextremesports.Headovertoabookseller's

website,likeBarnes&Noble(http://barnesandnoble.com)and

performasearchfor"extremesports";copytheaddressofthe

resultspage,showninFigure3-24.ThisURLmightlooklikethe

following:



http://search.barnesandnoble.com/booksearch/results.asp?WRD=ext

NowyouholdthefirstparttosuccessfullyusetheimportXML

function:theURLofthesourcepage.Thesecondpartofthe

equationistotellyourspreadsheetspecificallywhichHTMLpart

ofthatpageyouwant,byusingXPath,alanguagefor

specifyingsubsetsofanXML(orXHTML)document.Openthe

HTMLsourceoftheBarnes&Noblepagebyright-clickingthe

pageandpicking"ViewPageSource"fromthecontextmenu.

CheckfortheHTMLstructureinwhichthebooktitlesarelisted

onthepage.Inthiscase,thesurroundingHTMLlookslikethe

following(withindentionandlinebreaksaddedforclarity):









MaximumRide

...



Onewaytodescribethepositionofthetitleisthustosay:

anywhereintheHTML,withinadivtagoftheclass"bc-desc",

there'san"h2"tag,andinitan"a"tag—extractthetextwithin

it.InXPathsyntax,thisisexpressedas:

//div[@class='bc-desc']/h2/a/text()



Toexplain,thedoubleslashesinthebeginningmean"anywhere

withinthestructure,"whereasasingleslashmeans"achild

nodeimmediatelybelowthecurrentstructure."The@character

denotesanattribute.Andtext()grabsthetextnodeitself,

thoughit'soptionalfortheimportXmlfunction(still,it'sgood

practicetouseitincaseyouuseXPathinotherenvironments).

Tosummarize,thecellcontentyouwanttopasteintoyour

spreadsheetisthefollowing(allononeline):



=importXml("http://search.barnesandnoble.com/booksearch/results

.asp?WRD=extreme+sports",

"//div[@class='bc-desc']/h2/a/text()")



Insertthisformulaintoacell,pressReturnorEnterandwaita

fewseconds.Theresultisalistlikethefollowing,alsoshownin

Figure3-25:

ExtremeSports

HowAngelPetersonGotHisName

Winterdance

NoNestfortheWicket(MegLangslowSeries#7)

MaximumRide

BruceLee'sFightingMethod



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 26. Automatically Complete Lists of Related Items

Tải bản đầy đủ ngay(0 tr)

×