sheet.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import (
  11. "bytes"
  12. "encoding/json"
  13. "encoding/xml"
  14. "errors"
  15. "io/ioutil"
  16. "os"
  17. "path"
  18. "regexp"
  19. "strconv"
  20. "strings"
  21. "unicode/utf8"
  22. "github.com/mohae/deepcopy"
  23. )
  24. // NewSheet provides function to create a new sheet by given worksheet name.
  25. // When creating a new XLSX file, the default sheet will be created. Returns
  26. // the number of sheets in the workbook (file) after appending the new sheet.
  27. func (f *File) NewSheet(name string) int {
  28. // Check if the worksheet already exists
  29. if f.GetSheetIndex(name) != 0 {
  30. return f.SheetCount
  31. }
  32. f.DeleteSheet(name)
  33. f.SheetCount++
  34. wb := f.workbookReader()
  35. sheetID := 0
  36. for _, v := range wb.Sheets.Sheet {
  37. if v.SheetID > sheetID {
  38. sheetID = v.SheetID
  39. }
  40. }
  41. sheetID++
  42. // Update docProps/app.xml
  43. f.setAppXML()
  44. // Update [Content_Types].xml
  45. f.setContentTypes(sheetID)
  46. // Create new sheet /xl/worksheets/sheet%d.xml
  47. f.setSheet(sheetID, name)
  48. // Update xl/_rels/workbook.xml.rels
  49. rID := f.addXlsxWorkbookRels(sheetID)
  50. // Update xl/workbook.xml
  51. f.setWorkbook(name, sheetID, rID)
  52. return sheetID
  53. }
  54. // contentTypesReader provides a function to get the pointer to the
  55. // [Content_Types].xml structure after deserialization.
  56. func (f *File) contentTypesReader() *xlsxTypes {
  57. if f.ContentTypes == nil {
  58. var content xlsxTypes
  59. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("[Content_Types].xml")), &content)
  60. f.ContentTypes = &content
  61. }
  62. return f.ContentTypes
  63. }
  64. // contentTypesWriter provides a function to save [Content_Types].xml after
  65. // serialize structure.
  66. func (f *File) contentTypesWriter() {
  67. if f.ContentTypes != nil {
  68. output, _ := xml.Marshal(f.ContentTypes)
  69. f.saveFileList("[Content_Types].xml", output)
  70. }
  71. }
  72. // workbookReader provides a function to get the pointer to the xl/workbook.xml
  73. // structure after deserialization.
  74. func (f *File) workbookReader() *xlsxWorkbook {
  75. if f.WorkBook == nil {
  76. var content xlsxWorkbook
  77. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/workbook.xml")), &content)
  78. f.WorkBook = &content
  79. }
  80. return f.WorkBook
  81. }
  82. // workBookWriter provides a function to save xl/workbook.xml after serialize
  83. // structure.
  84. func (f *File) workBookWriter() {
  85. if f.WorkBook != nil {
  86. output, _ := xml.Marshal(f.WorkBook)
  87. f.saveFileList("xl/workbook.xml", replaceRelationshipsNameSpaceBytes(output))
  88. }
  89. }
  90. // workSheetWriter provides a function to save xl/worksheets/sheet%d.xml after
  91. // serialize structure.
  92. func (f *File) workSheetWriter() {
  93. for p, sheet := range f.Sheet {
  94. if sheet != nil {
  95. for k, v := range sheet.SheetData.Row {
  96. f.Sheet[p].SheetData.Row[k].C = trimCell(v.C)
  97. }
  98. output, _ := xml.Marshal(sheet)
  99. f.saveFileList(p, replaceWorkSheetsRelationshipsNameSpaceBytes(output))
  100. ok := f.checked[p]
  101. if ok {
  102. f.checked[p] = false
  103. }
  104. }
  105. }
  106. }
  107. // trimCell provides a function to trim blank cells which created by completeCol.
  108. func trimCell(column []xlsxC) []xlsxC {
  109. col := make([]xlsxC, len(column))
  110. i := 0
  111. for _, c := range column {
  112. if c.S != 0 || c.V != "" || c.F != nil || c.T != "" {
  113. col[i] = c
  114. i++
  115. }
  116. }
  117. return col[0:i]
  118. }
  119. // setContentTypes provides a function to read and update property of contents
  120. // type of XLSX.
  121. func (f *File) setContentTypes(index int) {
  122. content := f.contentTypesReader()
  123. content.Overrides = append(content.Overrides, xlsxOverride{
  124. PartName: "/xl/worksheets/sheet" + strconv.Itoa(index) + ".xml",
  125. ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
  126. })
  127. }
  128. // setSheet provides a function to update sheet property by given index.
  129. func (f *File) setSheet(index int, name string) {
  130. var xlsx xlsxWorksheet
  131. xlsx.Dimension.Ref = "A1"
  132. xlsx.SheetViews.SheetView = append(xlsx.SheetViews.SheetView, xlsxSheetView{
  133. WorkbookViewID: 0,
  134. })
  135. path := "xl/worksheets/sheet" + strconv.Itoa(index) + ".xml"
  136. f.sheetMap[trimSheetName(name)] = path
  137. f.Sheet[path] = &xlsx
  138. }
  139. // setWorkbook update workbook property of XLSX. Maximum 31 characters are
  140. // allowed in sheet title.
  141. func (f *File) setWorkbook(name string, sheetID, rid int) {
  142. content := f.workbookReader()
  143. content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{
  144. Name: trimSheetName(name),
  145. SheetID: sheetID,
  146. ID: "rId" + strconv.Itoa(rid),
  147. })
  148. }
  149. // workbookRelsReader provides a function to read and unmarshal workbook
  150. // relationships of XLSX file.
  151. func (f *File) workbookRelsReader() *xlsxWorkbookRels {
  152. if f.WorkBookRels == nil {
  153. var content xlsxWorkbookRels
  154. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/_rels/workbook.xml.rels")), &content)
  155. f.WorkBookRels = &content
  156. }
  157. return f.WorkBookRels
  158. }
  159. // workBookRelsWriter provides a function to save xl/_rels/workbook.xml.rels after
  160. // serialize structure.
  161. func (f *File) workBookRelsWriter() {
  162. if f.WorkBookRels != nil {
  163. output, _ := xml.Marshal(f.WorkBookRels)
  164. f.saveFileList("xl/_rels/workbook.xml.rels", output)
  165. }
  166. }
  167. // addXlsxWorkbookRels update workbook relationships property of XLSX.
  168. func (f *File) addXlsxWorkbookRels(sheet int) int {
  169. content := f.workbookRelsReader()
  170. rID := 0
  171. for _, v := range content.Relationships {
  172. t, _ := strconv.Atoi(strings.TrimPrefix(v.ID, "rId"))
  173. if t > rID {
  174. rID = t
  175. }
  176. }
  177. rID++
  178. ID := bytes.Buffer{}
  179. ID.WriteString("rId")
  180. ID.WriteString(strconv.Itoa(rID))
  181. target := bytes.Buffer{}
  182. target.WriteString("worksheets/sheet")
  183. target.WriteString(strconv.Itoa(sheet))
  184. target.WriteString(".xml")
  185. content.Relationships = append(content.Relationships, xlsxWorkbookRelation{
  186. ID: ID.String(),
  187. Target: target.String(),
  188. Type: SourceRelationshipWorkSheet,
  189. })
  190. return rID
  191. }
  192. // setAppXML update docProps/app.xml file of XML.
  193. func (f *File) setAppXML() {
  194. f.saveFileList("docProps/app.xml", []byte(templateDocpropsApp))
  195. }
  196. // replaceRelationshipsNameSpaceBytes; Some tools that read XLSX files have
  197. // very strict requirements about the structure of the input XML. In
  198. // particular both Numbers on the Mac and SAS dislike inline XML namespace
  199. // declarations, or namespace prefixes that don't match the ones that Excel
  200. // itself uses. This is a problem because the Go XML library doesn't multiple
  201. // namespace declarations in a single element of a document. This function is
  202. // a horrible hack to fix that after the XML marshalling is completed.
  203. func replaceRelationshipsNameSpaceBytes(workbookMarshal []byte) []byte {
  204. oldXmlns := []byte(`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`)
  205. newXmlns := []byte(`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">`)
  206. return bytes.Replace(workbookMarshal, oldXmlns, newXmlns, -1)
  207. }
  208. // SetActiveSheet provides function to set default active worksheet of XLSX by
  209. // given index. Note that active index is different from the index returned by
  210. // function GetSheetMap(). It should be greater than 0 and less than total
  211. // worksheet numbers.
  212. func (f *File) SetActiveSheet(index int) {
  213. if index < 1 {
  214. index = 1
  215. }
  216. wb := f.workbookReader()
  217. for activeTab, sheet := range wb.Sheets.Sheet {
  218. if sheet.SheetID == index {
  219. if len(wb.BookViews.WorkBookView) > 0 {
  220. wb.BookViews.WorkBookView[0].ActiveTab = activeTab
  221. } else {
  222. wb.BookViews.WorkBookView = append(wb.BookViews.WorkBookView, xlsxWorkBookView{
  223. ActiveTab: activeTab,
  224. })
  225. }
  226. }
  227. }
  228. for idx, name := range f.GetSheetMap() {
  229. xlsx := f.workSheetReader(name)
  230. if len(xlsx.SheetViews.SheetView) > 0 {
  231. xlsx.SheetViews.SheetView[0].TabSelected = false
  232. }
  233. if index == idx {
  234. if len(xlsx.SheetViews.SheetView) > 0 {
  235. xlsx.SheetViews.SheetView[0].TabSelected = true
  236. } else {
  237. xlsx.SheetViews.SheetView = append(xlsx.SheetViews.SheetView, xlsxSheetView{
  238. TabSelected: true,
  239. })
  240. }
  241. }
  242. }
  243. }
  244. // GetActiveSheetIndex provides a function to get active sheet index of the
  245. // XLSX. If not found the active sheet will be return integer 0.
  246. func (f *File) GetActiveSheetIndex() int {
  247. for idx, name := range f.GetSheetMap() {
  248. xlsx := f.workSheetReader(name)
  249. for _, sheetView := range xlsx.SheetViews.SheetView {
  250. if sheetView.TabSelected {
  251. return idx
  252. }
  253. }
  254. }
  255. return 0
  256. }
  257. // SetSheetName provides a function to set the worksheet name be given old and
  258. // new worksheet name. Maximum 31 characters are allowed in sheet title and
  259. // this function only changes the name of the sheet and will not update the
  260. // sheet name in the formula or reference associated with the cell. So there
  261. // may be problem formula error or reference missing.
  262. func (f *File) SetSheetName(oldName, newName string) {
  263. oldName = trimSheetName(oldName)
  264. newName = trimSheetName(newName)
  265. content := f.workbookReader()
  266. for k, v := range content.Sheets.Sheet {
  267. if v.Name == oldName {
  268. content.Sheets.Sheet[k].Name = newName
  269. f.sheetMap[newName] = f.sheetMap[oldName]
  270. delete(f.sheetMap, oldName)
  271. }
  272. }
  273. }
  274. // GetSheetName provides a function to get worksheet name of XLSX by given
  275. // worksheet index. If given sheet index is invalid, will return an empty
  276. // string.
  277. func (f *File) GetSheetName(index int) string {
  278. content := f.workbookReader()
  279. rels := f.workbookRelsReader()
  280. for _, rel := range rels.Relationships {
  281. rID, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rel.Target, "worksheets/sheet"), ".xml"))
  282. if rID == index {
  283. for _, v := range content.Sheets.Sheet {
  284. if v.ID == rel.ID {
  285. return v.Name
  286. }
  287. }
  288. }
  289. }
  290. return ""
  291. }
  292. // GetSheetIndex provides a function to get worksheet index of XLSX by given sheet
  293. // name. If given worksheet name is invalid, will return an integer type value
  294. // 0.
  295. func (f *File) GetSheetIndex(name string) int {
  296. content := f.workbookReader()
  297. rels := f.workbookRelsReader()
  298. for _, v := range content.Sheets.Sheet {
  299. if v.Name == name {
  300. for _, rel := range rels.Relationships {
  301. if v.ID == rel.ID {
  302. rID, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rel.Target, "worksheets/sheet"), ".xml"))
  303. return rID
  304. }
  305. }
  306. }
  307. }
  308. return 0
  309. }
  310. // GetSheetMap provides a function to get worksheet name and index map of XLSX.
  311. // For example:
  312. //
  313. // xlsx, err := excelize.OpenFile("./Book1.xlsx")
  314. // if err != nil {
  315. // return
  316. // }
  317. // for index, name := range xlsx.GetSheetMap() {
  318. // fmt.Println(index, name)
  319. // }
  320. //
  321. func (f *File) GetSheetMap() map[int]string {
  322. content := f.workbookReader()
  323. rels := f.workbookRelsReader()
  324. sheetMap := map[int]string{}
  325. for _, v := range content.Sheets.Sheet {
  326. for _, rel := range rels.Relationships {
  327. relStr := strings.SplitN(rel.Target, "worksheets/sheet", 2)
  328. if rel.ID == v.ID && len(relStr) == 2 {
  329. rID, _ := strconv.Atoi(strings.TrimSuffix(relStr[1], ".xml"))
  330. sheetMap[rID] = v.Name
  331. }
  332. }
  333. }
  334. return sheetMap
  335. }
  336. // getSheetMap provides a function to get worksheet name and XML file path map of
  337. // XLSX.
  338. func (f *File) getSheetMap() map[string]string {
  339. maps := make(map[string]string)
  340. for idx, name := range f.GetSheetMap() {
  341. maps[name] = "xl/worksheets/sheet" + strconv.Itoa(idx) + ".xml"
  342. }
  343. return maps
  344. }
  345. // SetSheetBackground provides a function to set background picture by given
  346. // worksheet name and file path.
  347. func (f *File) SetSheetBackground(sheet, picture string) error {
  348. var err error
  349. // Check picture exists first.
  350. if _, err = os.Stat(picture); os.IsNotExist(err) {
  351. return err
  352. }
  353. ext, ok := supportImageTypes[path.Ext(picture)]
  354. if !ok {
  355. return errors.New("unsupported image extension")
  356. }
  357. pictureID := f.countMedia() + 1
  358. rID := f.addSheetRelationships(sheet, SourceRelationshipImage, "../media/image"+strconv.Itoa(pictureID)+ext, "")
  359. f.addSheetPicture(sheet, rID)
  360. file, _ := ioutil.ReadFile(picture)
  361. f.addMedia(file, ext)
  362. f.setContentTypePartImageExtensions()
  363. return err
  364. }
  365. // DeleteSheet provides a function to delete worksheet in a workbook by given
  366. // worksheet name. Use this method with caution, which will affect changes in
  367. // references such as formulas, charts, and so on. If there is any referenced
  368. // value of the deleted worksheet, it will cause a file error when you open it.
  369. // This function will be invalid when only the one worksheet is left.
  370. func (f *File) DeleteSheet(name string) {
  371. content := f.workbookReader()
  372. for k, v := range content.Sheets.Sheet {
  373. if v.Name == trimSheetName(name) && len(content.Sheets.Sheet) > 1 {
  374. content.Sheets.Sheet = append(content.Sheets.Sheet[:k], content.Sheets.Sheet[k+1:]...)
  375. sheet := "xl/worksheets/sheet" + strconv.Itoa(v.SheetID) + ".xml"
  376. rels := "xl/worksheets/_rels/sheet" + strconv.Itoa(v.SheetID) + ".xml.rels"
  377. target := f.deleteSheetFromWorkbookRels(v.ID)
  378. f.deleteSheetFromContentTypes(target)
  379. f.deleteCalcChain(v.SheetID, "") // Delete CalcChain
  380. delete(f.sheetMap, name)
  381. delete(f.XLSX, sheet)
  382. delete(f.XLSX, rels)
  383. delete(f.Sheet, sheet)
  384. f.SheetCount--
  385. }
  386. }
  387. f.SetActiveSheet(len(f.GetSheetMap()))
  388. }
  389. // deleteSheetFromWorkbookRels provides a function to remove worksheet
  390. // relationships by given relationships ID in the file
  391. // xl/_rels/workbook.xml.rels.
  392. func (f *File) deleteSheetFromWorkbookRels(rID string) string {
  393. content := f.workbookRelsReader()
  394. for k, v := range content.Relationships {
  395. if v.ID == rID {
  396. content.Relationships = append(content.Relationships[:k], content.Relationships[k+1:]...)
  397. return v.Target
  398. }
  399. }
  400. return ""
  401. }
  402. // deleteSheetFromContentTypes provides a function to remove worksheet
  403. // relationships by given target name in the file [Content_Types].xml.
  404. func (f *File) deleteSheetFromContentTypes(target string) {
  405. content := f.contentTypesReader()
  406. for k, v := range content.Overrides {
  407. if v.PartName == "/xl/"+target {
  408. content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...)
  409. }
  410. }
  411. }
  412. // CopySheet provides a function to duplicate a worksheet by gave source and
  413. // target worksheet index. Note that currently doesn't support duplicate
  414. // workbooks that contain tables, charts or pictures. For Example:
  415. //
  416. // // Sheet1 already exists...
  417. // index := xlsx.NewSheet("Sheet2")
  418. // err := xlsx.CopySheet(1, index)
  419. // return err
  420. //
  421. func (f *File) CopySheet(from, to int) error {
  422. if from < 1 || to < 1 || from == to || f.GetSheetName(from) == "" || f.GetSheetName(to) == "" {
  423. return errors.New("invalid worksheet index")
  424. }
  425. f.copySheet(from, to)
  426. return nil
  427. }
  428. // copySheet provides a function to duplicate a worksheet by gave source and
  429. // target worksheet name.
  430. func (f *File) copySheet(from, to int) {
  431. sheet := f.workSheetReader("sheet" + strconv.Itoa(from))
  432. worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet)
  433. path := "xl/worksheets/sheet" + strconv.Itoa(to) + ".xml"
  434. if len(worksheet.SheetViews.SheetView) > 0 {
  435. worksheet.SheetViews.SheetView[0].TabSelected = false
  436. }
  437. worksheet.Drawing = nil
  438. worksheet.TableParts = nil
  439. worksheet.PageSetUp = nil
  440. f.Sheet[path] = worksheet
  441. toRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(to) + ".xml.rels"
  442. fromRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(from) + ".xml.rels"
  443. _, ok := f.XLSX[fromRels]
  444. if ok {
  445. f.XLSX[toRels] = f.XLSX[fromRels]
  446. }
  447. }
  448. // SetSheetVisible provides a function to set worksheet visible by given worksheet
  449. // name. A workbook must contain at least one visible worksheet. If the given
  450. // worksheet has been activated, this setting will be invalidated. Sheet state
  451. // values as defined by http://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.sheetstatevalues.aspx
  452. //
  453. // visible
  454. // hidden
  455. // veryHidden
  456. //
  457. // For example, hide Sheet1:
  458. //
  459. // xlsx.SetSheetVisible("Sheet1", false)
  460. //
  461. func (f *File) SetSheetVisible(name string, visible bool) {
  462. name = trimSheetName(name)
  463. content := f.workbookReader()
  464. if visible {
  465. for k, v := range content.Sheets.Sheet {
  466. if v.Name == name {
  467. content.Sheets.Sheet[k].State = ""
  468. }
  469. }
  470. return
  471. }
  472. count := 0
  473. for _, v := range content.Sheets.Sheet {
  474. if v.State != "hidden" {
  475. count++
  476. }
  477. }
  478. for k, v := range content.Sheets.Sheet {
  479. xlsx := f.workSheetReader(f.GetSheetMap()[k])
  480. tabSelected := false
  481. if len(xlsx.SheetViews.SheetView) > 0 {
  482. tabSelected = xlsx.SheetViews.SheetView[0].TabSelected
  483. }
  484. if v.Name == name && count > 1 && !tabSelected {
  485. content.Sheets.Sheet[k].State = "hidden"
  486. }
  487. }
  488. }
  489. // parseFormatPanesSet provides a function to parse the panes settings.
  490. func parseFormatPanesSet(formatSet string) (*formatPanes, error) {
  491. format := formatPanes{}
  492. err := json.Unmarshal([]byte(formatSet), &format)
  493. return &format, err
  494. }
  495. // SetPanes provides a function to create and remove freeze panes and split panes
  496. // by given worksheet name and panes format set.
  497. //
  498. // activePane defines the pane that is active. The possible values for this
  499. // attribute are defined in the following table:
  500. //
  501. // Enumeration Value | Description
  502. // --------------------------------+-------------------------------------------------------------
  503. // bottomLeft (Bottom Left Pane) | Bottom left pane, when both vertical and horizontal
  504. // | splits are applied.
  505. // |
  506. // | This value is also used when only a horizontal split has
  507. // | been applied, dividing the pane into upper and lower
  508. // | regions. In that case, this value specifies the bottom
  509. // | pane.
  510. // |
  511. // bottomRight (Bottom Right Pane) | Bottom right pane, when both vertical and horizontal
  512. // | splits are applied.
  513. // |
  514. // topLeft (Top Left Pane) | Top left pane, when both vertical and horizontal splits
  515. // | are applied.
  516. // |
  517. // | This value is also used when only a horizontal split has
  518. // | been applied, dividing the pane into upper and lower
  519. // | regions. In that case, this value specifies the top pane.
  520. // |
  521. // | This value is also used when only a vertical split has
  522. // | been applied, dividing the pane into right and left
  523. // | regions. In that case, this value specifies the left pane
  524. // |
  525. // topRight (Top Right Pane) | Top right pane, when both vertical and horizontal
  526. // | splits are applied.
  527. // |
  528. // | This value is also used when only a vertical split has
  529. // | been applied, dividing the pane into right and left
  530. // | regions. In that case, this value specifies the right
  531. // | pane.
  532. //
  533. // Pane state type is restricted to the values supported currently listed in the following table:
  534. //
  535. // Enumeration Value | Description
  536. // --------------------------------+-------------------------------------------------------------
  537. // frozen (Frozen) | Panes are frozen, but were not split being frozen. In
  538. // | this state, when the panes are unfrozen again, a single
  539. // | pane results, with no split.
  540. // |
  541. // | In this state, the split bars are not adjustable.
  542. // |
  543. // split (Split) | Panes are split, but not frozen. In this state, the split
  544. // | bars are adjustable by the user.
  545. //
  546. // x_split (Horizontal Split Position): Horizontal position of the split, in
  547. // 1/20th of a point; 0 (zero) if none. If the pane is frozen, this value
  548. // indicates the number of columns visible in the top pane.
  549. //
  550. // y_split (Vertical Split Position): Vertical position of the split, in 1/20th
  551. // of a point; 0 (zero) if none. If the pane is frozen, this value indicates the
  552. // number of rows visible in the left pane. The possible values for this
  553. // attribute are defined by the W3C XML Schema double datatype.
  554. //
  555. // top_left_cell: Location of the top left visible cell in the bottom right pane
  556. // (when in Left-To-Right mode).
  557. //
  558. // sqref (Sequence of References): Range of the selection. Can be non-contiguous
  559. // set of ranges.
  560. //
  561. // An example of how to freeze column A in the Sheet1 and set the active cell on
  562. // Sheet1!K16:
  563. //
  564. // xlsx.SetPanes("Sheet1", `{"freeze":true,"split":false,"x_split":1,"y_split":0,"top_left_cell":"B1","active_pane":"topRight","panes":[{"sqref":"K16","active_cell":"K16","pane":"topRight"}]}`)
  565. //
  566. // An example of how to freeze rows 1 to 9 in the Sheet1 and set the active cell
  567. // ranges on Sheet1!A11:XFD11:
  568. //
  569. // xlsx.SetPanes("Sheet1", `{"freeze":true,"split":false,"x_split":0,"y_split":9,"top_left_cell":"A34","active_pane":"bottomLeft","panes":[{"sqref":"A11:XFD11","active_cell":"A11","pane":"bottomLeft"}]}`)
  570. //
  571. // An example of how to create split panes in the Sheet1 and set the active cell
  572. // on Sheet1!J60:
  573. //
  574. // xlsx.SetPanes("Sheet1", `{"freeze":false,"split":true,"x_split":3270,"y_split":1800,"top_left_cell":"N57","active_pane":"bottomLeft","panes":[{"sqref":"I36","active_cell":"I36"},{"sqref":"G33","active_cell":"G33","pane":"topRight"},{"sqref":"J60","active_cell":"J60","pane":"bottomLeft"},{"sqref":"O60","active_cell":"O60","pane":"bottomRight"}]}`)
  575. //
  576. // An example of how to unfreeze and remove all panes on Sheet1:
  577. //
  578. // xlsx.SetPanes("Sheet1", `{"freeze":false,"split":false}`)
  579. //
  580. func (f *File) SetPanes(sheet, panes string) {
  581. fs, _ := parseFormatPanesSet(panes)
  582. xlsx := f.workSheetReader(sheet)
  583. p := &xlsxPane{
  584. ActivePane: fs.ActivePane,
  585. TopLeftCell: fs.TopLeftCell,
  586. XSplit: float64(fs.XSplit),
  587. YSplit: float64(fs.YSplit),
  588. }
  589. if fs.Freeze {
  590. p.State = "frozen"
  591. }
  592. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Pane = p
  593. if !(fs.Freeze) && !(fs.Split) {
  594. if len(xlsx.SheetViews.SheetView) > 0 {
  595. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Pane = nil
  596. }
  597. }
  598. s := []*xlsxSelection{}
  599. for _, p := range fs.Panes {
  600. s = append(s, &xlsxSelection{
  601. ActiveCell: p.ActiveCell,
  602. Pane: p.Pane,
  603. SQRef: p.SQRef,
  604. })
  605. }
  606. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Selection = s
  607. }
  608. // GetSheetVisible provides a function to get worksheet visible by given worksheet
  609. // name. For example, get visible state of Sheet1:
  610. //
  611. // xlsx.GetSheetVisible("Sheet1")
  612. //
  613. func (f *File) GetSheetVisible(name string) bool {
  614. content := f.workbookReader()
  615. visible := false
  616. for k, v := range content.Sheets.Sheet {
  617. if v.Name == trimSheetName(name) {
  618. if content.Sheets.Sheet[k].State == "" || content.Sheets.Sheet[k].State == "visible" {
  619. visible = true
  620. }
  621. }
  622. }
  623. return visible
  624. }
  625. // SearchSheet provides a function to get coordinates by given worksheet name,
  626. // cell value, and regular expression. The function doesn't support searching
  627. // on the calculated result, formatted numbers and conditional lookup
  628. // currently. If it is a merged cell, it will return the coordinates of the
  629. // upper left corner of the merged area.
  630. //
  631. // An example of search the coordinates of the value of "100" on Sheet1:
  632. //
  633. // result, err := xlsx.SearchSheet("Sheet1", "100")
  634. //
  635. // An example of search the coordinates where the numerical value in the range
  636. // of "0-9" of Sheet1 is described:
  637. //
  638. // result, err := xlsx.SearchSheet("Sheet1", "[0-9]", true)
  639. //
  640. func (f *File) SearchSheet(sheet, value string, reg ...bool) ([]string, error) {
  641. var regSearch bool
  642. for _, r := range reg {
  643. regSearch = r
  644. }
  645. xlsx := f.workSheetReader(sheet)
  646. var (
  647. result []string
  648. )
  649. name, ok := f.sheetMap[trimSheetName(sheet)]
  650. if !ok {
  651. return result, nil
  652. }
  653. if xlsx != nil {
  654. output, _ := xml.Marshal(f.Sheet[name])
  655. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpaceBytes(output))
  656. }
  657. xml.NewDecoder(bytes.NewReader(f.readXML(name)))
  658. d := f.sharedStringsReader()
  659. var inElement string
  660. var r xlsxRow
  661. decoder := xml.NewDecoder(bytes.NewReader(f.readXML(name)))
  662. for {
  663. token, _ := decoder.Token()
  664. if token == nil {
  665. break
  666. }
  667. switch startElement := token.(type) {
  668. case xml.StartElement:
  669. inElement = startElement.Name.Local
  670. if inElement == "row" {
  671. r = xlsxRow{}
  672. _ = decoder.DecodeElement(&r, &startElement)
  673. for _, colCell := range r.C {
  674. val, _ := colCell.getValueFrom(f, d)
  675. if regSearch {
  676. regex := regexp.MustCompile(value)
  677. if !regex.MatchString(val) {
  678. continue
  679. }
  680. } else {
  681. if val != value {
  682. continue
  683. }
  684. }
  685. cellCol, _, err := CellNameToCoordinates(colCell.R)
  686. if err != nil {
  687. return result, err
  688. }
  689. cellName, err := CoordinatesToCellName(cellCol, r.R)
  690. if err != nil {
  691. return result, err
  692. }
  693. result = append(result, cellName)
  694. }
  695. }
  696. default:
  697. }
  698. }
  699. return result, nil
  700. }
  701. // ProtectSheet provides a function to prevent other users from accidentally
  702. // or deliberately changing, moving, or deleting data in a worksheet. For
  703. // example, protect Sheet1 with protection settings:
  704. //
  705. // xlsx.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{
  706. // Password: "password",
  707. // EditScenarios: false,
  708. // })
  709. //
  710. func (f *File) ProtectSheet(sheet string, settings *FormatSheetProtection) {
  711. xlsx := f.workSheetReader(sheet)
  712. if settings == nil {
  713. settings = &FormatSheetProtection{
  714. EditObjects: true,
  715. EditScenarios: true,
  716. SelectLockedCells: true,
  717. }
  718. }
  719. xlsx.SheetProtection = &xlsxSheetProtection{
  720. AutoFilter: settings.AutoFilter,
  721. DeleteColumns: settings.DeleteColumns,
  722. DeleteRows: settings.DeleteRows,
  723. FormatCells: settings.FormatCells,
  724. FormatColumns: settings.FormatColumns,
  725. FormatRows: settings.FormatRows,
  726. InsertColumns: settings.InsertColumns,
  727. InsertHyperlinks: settings.InsertHyperlinks,
  728. InsertRows: settings.InsertRows,
  729. Objects: settings.EditObjects,
  730. PivotTables: settings.PivotTables,
  731. Scenarios: settings.EditScenarios,
  732. SelectLockedCells: settings.SelectLockedCells,
  733. SelectUnlockedCells: settings.SelectUnlockedCells,
  734. Sheet: true,
  735. Sort: settings.Sort,
  736. }
  737. if settings.Password != "" {
  738. xlsx.SheetProtection.Password = genSheetPasswd(settings.Password)
  739. }
  740. }
  741. // UnprotectSheet provides a function to unprotect an Excel worksheet.
  742. func (f *File) UnprotectSheet(sheet string) {
  743. xlsx := f.workSheetReader(sheet)
  744. xlsx.SheetProtection = nil
  745. }
  746. // trimSheetName provides a function to trim invaild characters by given worksheet
  747. // name.
  748. func trimSheetName(name string) string {
  749. if strings.ContainsAny(name, ":\\/?*[]") || utf8.RuneCountInString(name) > 31 {
  750. r := make([]rune, 0, 31)
  751. for _, v := range name {
  752. switch v {
  753. case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[]
  754. continue
  755. default:
  756. r = append(r, v)
  757. }
  758. if len(r) == 31 {
  759. break
  760. }
  761. }
  762. name = string(r)
  763. }
  764. return name
  765. }
  766. // PageLayoutOption is an option of a page layout of a worksheet. See
  767. // SetPageLayout().
  768. type PageLayoutOption interface {
  769. setPageLayout(layout *xlsxPageSetUp)
  770. }
  771. // PageLayoutOptionPtr is a writable PageLayoutOption. See GetPageLayout().
  772. type PageLayoutOptionPtr interface {
  773. PageLayoutOption
  774. getPageLayout(layout *xlsxPageSetUp)
  775. }
  776. type (
  777. // PageLayoutOrientation defines the orientation of page layout for a
  778. // worksheet.
  779. PageLayoutOrientation string
  780. // PageLayoutPaperSize defines the paper size of the worksheet
  781. PageLayoutPaperSize int
  782. )
  783. const (
  784. // OrientationPortrait indicates page layout orientation id portrait.
  785. OrientationPortrait = "portrait"
  786. // OrientationLandscape indicates page layout orientation id landscape.
  787. OrientationLandscape = "landscape"
  788. )
  789. // setPageLayout provides a method to set the orientation for the worksheet.
  790. func (o PageLayoutOrientation) setPageLayout(ps *xlsxPageSetUp) {
  791. ps.Orientation = string(o)
  792. }
  793. // getPageLayout provides a method to get the orientation for the worksheet.
  794. func (o *PageLayoutOrientation) getPageLayout(ps *xlsxPageSetUp) {
  795. // Excel default: portrait
  796. if ps == nil || ps.Orientation == "" {
  797. *o = OrientationPortrait
  798. return
  799. }
  800. *o = PageLayoutOrientation(ps.Orientation)
  801. }
  802. // setPageLayout provides a method to set the paper size for the worksheet.
  803. func (p PageLayoutPaperSize) setPageLayout(ps *xlsxPageSetUp) {
  804. ps.PaperSize = int(p)
  805. }
  806. // getPageLayout provides a method to get the paper size for the worksheet.
  807. func (p *PageLayoutPaperSize) getPageLayout(ps *xlsxPageSetUp) {
  808. // Excel default: 1
  809. if ps == nil || ps.PaperSize == 0 {
  810. *p = 1
  811. return
  812. }
  813. *p = PageLayoutPaperSize(ps.PaperSize)
  814. }
  815. // SetPageLayout provides a function to sets worksheet page layout.
  816. //
  817. // Available options:
  818. // PageLayoutOrientation(string)
  819. // PageLayoutPaperSize(int)
  820. //
  821. // The following shows the paper size sorted by excelize index number:
  822. //
  823. // Index | Paper Size
  824. // -------+-----------------------------------------------
  825. // 1 | Letter paper (8.5 in. by 11 in.)
  826. // 2 | Letter small paper (8.5 in. by 11 in.)
  827. // 3 | Tabloid paper (11 in. by 17 in.)
  828. // 4 | Ledger paper (17 in. by 11 in.)
  829. // 5 | Legal paper (8.5 in. by 14 in.)
  830. // 6 | Statement paper (5.5 in. by 8.5 in.)
  831. // 7 | Executive paper (7.25 in. by 10.5 in.)
  832. // 8 | A3 paper (297 mm by 420 mm)
  833. // 9 | A4 paper (210 mm by 297 mm)
  834. // 10 | A4 small paper (210 mm by 297 mm)
  835. // 11 | A5 paper (148 mm by 210 mm)
  836. // 12 | B4 paper (250 mm by 353 mm)
  837. // 13 | B5 paper (176 mm by 250 mm)
  838. // 14 | Folio paper (8.5 in. by 13 in.)
  839. // 15 | Quarto paper (215 mm by 275 mm)
  840. // 16 | Standard paper (10 in. by 14 in.)
  841. // 17 | Standard paper (11 in. by 17 in.)
  842. // 18 | Note paper (8.5 in. by 11 in.)
  843. // 19 | #9 envelope (3.875 in. by 8.875 in.)
  844. // 20 | #10 envelope (4.125 in. by 9.5 in.)
  845. // 21 | #11 envelope (4.5 in. by 10.375 in.)
  846. // 22 | #12 envelope (4.75 in. by 11 in.)
  847. // 23 | #14 envelope (5 in. by 11.5 in.)
  848. // 24 | C paper (17 in. by 22 in.)
  849. // 25 | D paper (22 in. by 34 in.)
  850. // 26 | E paper (34 in. by 44 in.)
  851. // 27 | DL envelope (110 mm by 220 mm)
  852. // 28 | C5 envelope (162 mm by 229 mm)
  853. // 29 | C3 envelope (324 mm by 458 mm)
  854. // 30 | C4 envelope (229 mm by 324 mm)
  855. // 31 | C6 envelope (114 mm by 162 mm)
  856. // 32 | C65 envelope (114 mm by 229 mm)
  857. // 33 | B4 envelope (250 mm by 353 mm)
  858. // 34 | B5 envelope (176 mm by 250 mm)
  859. // 35 | B6 envelope (176 mm by 125 mm)
  860. // 36 | Italy envelope (110 mm by 230 mm)
  861. // 37 | Monarch envelope (3.875 in. by 7.5 in.).
  862. // 38 | 6 3/4 envelope (3.625 in. by 6.5 in.)
  863. // 39 | US standard fanfold (14.875 in. by 11 in.)
  864. // 40 | German standard fanfold (8.5 in. by 12 in.)
  865. // 41 | German legal fanfold (8.5 in. by 13 in.)
  866. // 42 | ISO B4 (250 mm by 353 mm)
  867. // 43 | Japanese postcard (100 mm by 148 mm)
  868. // 44 | Standard paper (9 in. by 11 in.)
  869. // 45 | Standard paper (10 in. by 11 in.)
  870. // 46 | Standard paper (15 in. by 11 in.)
  871. // 47 | Invite envelope (220 mm by 220 mm)
  872. // 50 | Letter extra paper (9.275 in. by 12 in.)
  873. // 51 | Legal extra paper (9.275 in. by 15 in.)
  874. // 52 | Tabloid extra paper (11.69 in. by 18 in.)
  875. // 53 | A4 extra paper (236 mm by 322 mm)
  876. // 54 | Letter transverse paper (8.275 in. by 11 in.)
  877. // 55 | A4 transverse paper (210 mm by 297 mm)
  878. // 56 | Letter extra transverse paper (9.275 in. by 12 in.)
  879. // 57 | SuperA/SuperA/A4 paper (227 mm by 356 mm)
  880. // 58 | SuperB/SuperB/A3 paper (305 mm by 487 mm)
  881. // 59 | Letter plus paper (8.5 in. by 12.69 in.)
  882. // 60 | A4 plus paper (210 mm by 330 mm)
  883. // 61 | A5 transverse paper (148 mm by 210 mm)
  884. // 62 | JIS B5 transverse paper (182 mm by 257 mm)
  885. // 63 | A3 extra paper (322 mm by 445 mm)
  886. // 64 | A5 extra paper (174 mm by 235 mm)
  887. // 65 | ISO B5 extra paper (201 mm by 276 mm)
  888. // 66 | A2 paper (420 mm by 594 mm)
  889. // 67 | A3 transverse paper (297 mm by 420 mm)
  890. // 68 | A3 extra transverse paper (322 mm by 445 mm)
  891. // 69 | Japanese Double Postcard (200 mm x 148 mm)
  892. // 70 | A6 (105 mm x 148 mm)
  893. // 71 | Japanese Envelope Kaku #2
  894. // 72 | Japanese Envelope Kaku #3
  895. // 73 | Japanese Envelope Chou #3
  896. // 74 | Japanese Envelope Chou #4
  897. // 75 | Letter Rotated (11in x 8 1/2 11 in)
  898. // 76 | A3 Rotated (420 mm x 297 mm)
  899. // 77 | A4 Rotated (297 mm x 210 mm)
  900. // 78 | A5 Rotated (210 mm x 148 mm)
  901. // 79 | B4 (JIS) Rotated (364 mm x 257 mm)
  902. // 80 | B5 (JIS) Rotated (257 mm x 182 mm)
  903. // 81 | Japanese Postcard Rotated (148 mm x 100 mm)
  904. // 82 | Double Japanese Postcard Rotated (148 mm x 200 mm)
  905. // 83 | A6 Rotated (148 mm x 105 mm)
  906. // 84 | Japanese Envelope Kaku #2 Rotated
  907. // 85 | Japanese Envelope Kaku #3 Rotated
  908. // 86 | Japanese Envelope Chou #3 Rotated
  909. // 87 | Japanese Envelope Chou #4 Rotated
  910. // 88 | B6 (JIS) (128 mm x 182 mm)
  911. // 89 | B6 (JIS) Rotated (182 mm x 128 mm)
  912. // 90 | (12 in x 11 in)
  913. // 91 | Japanese Envelope You #4
  914. // 92 | Japanese Envelope You #4 Rotated
  915. // 93 | PRC 16K (146 mm x 215 mm)
  916. // 94 | PRC 32K (97 mm x 151 mm)
  917. // 95 | PRC 32K(Big) (97 mm x 151 mm)
  918. // 96 | PRC Envelope #1 (102 mm x 165 mm)
  919. // 97 | PRC Envelope #2 (102 mm x 176 mm)
  920. // 98 | PRC Envelope #3 (125 mm x 176 mm)
  921. // 99 | PRC Envelope #4 (110 mm x 208 mm)
  922. // 100 | PRC Envelope #5 (110 mm x 220 mm)
  923. // 101 | PRC Envelope #6 (120 mm x 230 mm)
  924. // 102 | PRC Envelope #7 (160 mm x 230 mm)
  925. // 103 | PRC Envelope #8 (120 mm x 309 mm)
  926. // 104 | PRC Envelope #9 (229 mm x 324 mm)
  927. // 105 | PRC Envelope #10 (324 mm x 458 mm)
  928. // 106 | PRC 16K Rotated
  929. // 107 | PRC 32K Rotated
  930. // 108 | PRC 32K(Big) Rotated
  931. // 109 | PRC Envelope #1 Rotated (165 mm x 102 mm)
  932. // 110 | PRC Envelope #2 Rotated (176 mm x 102 mm)
  933. // 111 | PRC Envelope #3 Rotated (176 mm x 125 mm)
  934. // 112 | PRC Envelope #4 Rotated (208 mm x 110 mm)
  935. // 113 | PRC Envelope #5 Rotated (220 mm x 110 mm)
  936. // 114 | PRC Envelope #6 Rotated (230 mm x 120 mm)
  937. // 115 | PRC Envelope #7 Rotated (230 mm x 160 mm)
  938. // 116 | PRC Envelope #8 Rotated (309 mm x 120 mm)
  939. // 117 | PRC Envelope #9 Rotated (324 mm x 229 mm)
  940. // 118 | PRC Envelope #10 Rotated (458 mm x 324 mm)
  941. //
  942. func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error {
  943. s := f.workSheetReader(sheet)
  944. ps := s.PageSetUp
  945. if ps == nil {
  946. ps = new(xlsxPageSetUp)
  947. s.PageSetUp = ps
  948. }
  949. for _, opt := range opts {
  950. opt.setPageLayout(ps)
  951. }
  952. return nil
  953. }
  954. // GetPageLayout provides a function to gets worksheet page layout.
  955. //
  956. // Available options:
  957. // PageLayoutOrientation(string)
  958. // PageLayoutPaperSize(int)
  959. func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error {
  960. s := f.workSheetReader(sheet)
  961. ps := s.PageSetUp
  962. for _, opt := range opts {
  963. opt.getPageLayout(ps)
  964. }
  965. return nil
  966. }
  967. // workSheetRelsReader provides a function to get the pointer to the structure
  968. // after deserialization of xl/worksheets/_rels/sheet%d.xml.rels.
  969. func (f *File) workSheetRelsReader(path string) *xlsxWorkbookRels {
  970. if f.WorkSheetRels[path] == nil {
  971. _, ok := f.XLSX[path]
  972. if ok {
  973. c := xlsxWorkbookRels{}
  974. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(path)), &c)
  975. f.WorkSheetRels[path] = &c
  976. }
  977. }
  978. return f.WorkSheetRels[path]
  979. }
  980. // workSheetRelsWriter provides a function to save
  981. // xl/worksheets/_rels/sheet%d.xml.rels after serialize structure.
  982. func (f *File) workSheetRelsWriter() {
  983. for p, r := range f.WorkSheetRels {
  984. if r != nil {
  985. v, _ := xml.Marshal(r)
  986. f.saveFileList(p, v)
  987. }
  988. }
  989. }
  990. // fillSheetData fill missing row and cell XML data to made it continuous from
  991. // first cell [1, 1] to last cell [col, row]
  992. func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) {
  993. rowCount := len(xlsx.SheetData.Row)
  994. if rowCount < row {
  995. // append missing rows
  996. for rowIdx := rowCount; rowIdx < row; rowIdx++ {
  997. xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1})
  998. }
  999. }
  1000. for rowIdx := range xlsx.SheetData.Row {
  1001. rowData := &xlsx.SheetData.Row[rowIdx] // take reference
  1002. cellCount := len(rowData.C)
  1003. if cellCount < col {
  1004. for colIdx := cellCount; colIdx < col; colIdx++ {
  1005. cellName, _ := CoordinatesToCellName(colIdx+1, rowIdx+1)
  1006. rowData.C = append(rowData.C, xlsxC{R: cellName})
  1007. }
  1008. }
  1009. }
  1010. }