Escolar Documentos
Profissional Documentos
Cultura Documentos
English |Signin
Search
e.g."templates"or"datastore"
GoogleAppsScript
GoogleAppsScript
UserGuide
GettingStarted
WritingScripts
CommonTasks
Events *
Tutorial:CreatingYourFirstSpreadsheetScript
HugoFierro,GoogleAppsScriptTeam
March2009
Goal
ThistutorialguidesyouthroughthestepsofcreatingyourfirstworkingSpreadsheetScript.Itcoversbasicreadingandwriting
Spreadsheetcellsandcreatingdialogboxes.
UserInterfaces *
TimetoComplete
UsingGUIBuilder *
Approximately10minutes
Security
Troubleshooting
AllServices
BaseServices
Prerequisites
Beforeyoubeginthistutorial,youjustneedanopenedGoogleSpreadsheet.
CalendarServices
ItisalsoniceifyouhavesomeexperienceusingtheJavaScriptlanguage,butifyoudon't,youshouldbeabletoeasilyunderstand
thisexample.
ContactsServices
Index
DocsListServices*
Thistutorialisdividedintothefollowingsections:
l Section1:UsingtheScriptEditor
DocumentServices*
FinanceServices
Section2:LearntheSpreadsheetAPIbasics
Summary
GmailServices*
JdbcServices*
LanguageServices
Section1:UsingtheScriptEditor
1. InyourSpreadsheet,clickonthe'Tools'menu,thenselect'Scripts'and'Scripteditor...'.
MailServices
MapsServices*
PropertiesServices*
SitesServices
SoapServices
SpreadsheetServices
UiServices*
UrlFetchServices
UtilitiesServices
XmlServices
Tutorials
Templates
ReleaseNotes
*Experimentalservices.
TheScripteditorwilpopupasanewwindow.MovethewindowssothatyoucansimulaneouslyvisualizeboththeScript
EditorandtheSpreadsheet.
Tutorials
Templates
TheScripteditorwilpopupasanewwindow.MovethewindowssothatyoucansimulaneouslyvisualizeboththeScript
EditorandtheSpreadsheet.
ReleaseNotes
*Experimentalservices.
2. CopyandPastethecodebelowinthetextbox,thenclickonthe'File'menu,then'Save'andgiveyournewScriptaname(e.g.
'myFirstScript').
functionexample(){
varss=SpreadsheetApp.getActiveSpreadsheet()
varsheet=ss.getSheets()[0]
varmyValue=Browser.inputBox("Enteranumber")
sheet.getRange("A1").setValue("Numberentered:")
varb1Range=sheet.getRange("B1")
b1Range.setValue(myValue)
varvalueToShow=b1Range.getValue()+1
Browser.msgBox("Thevalueyouenteredplusoneis:"+valueToShow)
}
3. Wewillanalyzethecodeinthenextsection,soletssimplyrunthescriptandseewhathappens!Selectthefunctionexample
fromtheComboboxatthetopoftheEditorthenclickonthe'Run'menu,then'Runselectedscript'.
4. AdialogboxshouldpopupintheSpreadsheetandrequestyoutoenteranumber.Enteroneandclick'Ok'.
Then,thespreadsheetcellsA1andB1areupdated.Finally,aseconddialogboxshowsthevalueyouenteredplus1.
ContinuereadingSection2tolearnmoreabouthowtheexamplescriptworks.
Section2:LearntheSpreadsheetAPIbasics
Inthissection,wewillanalyzelinebylinewhatthescriptisdoing.
ContinuereadingSection2tolearnmoreabouthowtheexamplescriptworks.
Section2:LearntheSpreadsheetAPIbasics
Inthissection,wewillanalyzelinebylinewhatthescriptisdoing.
functionexample(){
//code
}
UsersdefineJavaScriptfunctions.ThosecanbecalledfromtheScriptEditor,fromaSpreadsheetMenuorevendirectlyfromacell.
ThistutorialusesthemainthreetypesofSpreadsheetobjects:
l Spreadsheetobject
Sheetobject
l
l
Rangeobject
varss=SpreadsheetApp.getActiveSpreadsheet()
ThecodeabovegetsareferencetotheopenedSpreadsheetobject.
varsheet=ss.getSheets()[0]
ThecodeabovegetsareferencetothefirstSheetobjectinaSpreadsheet.
varmyValue=Browser.inputBox("Enteranumber")
ThecodeabovedisplaysaninputboxintheSpreadsheetandreturnswhatevertheuserwritesthere.
sheet.getRange("A1").setValue("Numberentered:")
Thecodeabovedoestwothingsinasinglelineofcode:
l getsarangeobjectforthecellA1
l
setsthevalueofthecellA1tothespecifiedstring
varb1Range=sheet.getRange("B1")
ThecodeabovegetsareferencetoaRangeobjectthatcontainsthecellB1.
b1Range.setValue(myValue)
Thecodeabovesetsthevalueofallthecellsintherangeb1Range.
varvalueToShow=b1Range.getValue()+1
ThecodeaboveaddsonetothevalueofthecellB1.
Browser.msgBox("Thevalueyouenteredplusoneis:"+valueToShow)
Thecodeabove:
l
l
concatenatesastringconstantwithanumber
popsupamessageboxthatshowsastring
Summary
Congratulations,you'vecompletedthistutorialandhavecreatedyourownSpreadsheetScript.Formoreinformationonaddingmore
functionality,readthroughthedocumentation.Postyourquestionsandcommentshere!
2011GoogleCodeHomeSiteTermsofServicePrivacyPolicySiteDirectory
GoogleCodeofferedin:EnglishEspaol PortugusP () ( )