sheet.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. // f, err := excelize.OpenFile("./Book1.xlsx")
  314. // if err != nil {
  315. // return
  316. // }
  317. // for index, name := range f.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. file, _ := ioutil.ReadFile(picture)
  358. name := f.addMedia(file, ext)
  359. rID := f.addSheetRelationships(sheet, SourceRelationshipImage, strings.Replace(name, "xl", "..", 1), "")
  360. f.addSheetPicture(sheet, rID)
  361. f.setContentTypePartImageExtensions()
  362. return err
  363. }
  364. // DeleteSheet provides a function to delete worksheet in a workbook by given
  365. // worksheet name. Use this method with caution, which will affect changes in
  366. // references such as formulas, charts, and so on. If there is any referenced
  367. // value of the deleted worksheet, it will cause a file error when you open it.
  368. // This function will be invalid when only the one worksheet is left.
  369. func (f *File) DeleteSheet(name string) {
  370. content := f.workbookReader()
  371. for k, v := range content.Sheets.Sheet {
  372. if v.Name == trimSheetName(name) && len(content.Sheets.Sheet) > 1 {
  373. content.Sheets.Sheet = append(content.Sheets.Sheet[:k], content.Sheets.Sheet[k+1:]...)
  374. sheet := "xl/worksheets/sheet" + strconv.Itoa(v.SheetID) + ".xml"
  375. rels := "xl/worksheets/_rels/sheet" + strconv.Itoa(v.SheetID) + ".xml.rels"
  376. target := f.deleteSheetFromWorkbookRels(v.ID)
  377. f.deleteSheetFromContentTypes(target)
  378. f.deleteCalcChain(v.SheetID, "") // Delete CalcChain
  379. delete(f.sheetMap, name)
  380. delete(f.XLSX, sheet)
  381. delete(f.XLSX, rels)
  382. delete(f.Sheet, sheet)
  383. f.SheetCount--
  384. }
  385. }
  386. f.SetActiveSheet(len(f.GetSheetMap()))
  387. }
  388. // deleteSheetFromWorkbookRels provides a function to remove worksheet
  389. // relationships by given relationships ID in the file
  390. // xl/_rels/workbook.xml.rels.
  391. func (f *File) deleteSheetFromWorkbookRels(rID string) string {
  392. content := f.workbookRelsReader()
  393. for k, v := range content.Relationships {
  394. if v.ID == rID {
  395. content.Relationships = append(content.Relationships[:k], content.Relationships[k+1:]...)
  396. return v.Target
  397. }
  398. }
  399. return ""
  400. }
  401. // deleteSheetFromContentTypes provides a function to remove worksheet
  402. // relationships by given target name in the file [Content_Types].xml.
  403. func (f *File) deleteSheetFromContentTypes(target string) {
  404. content := f.contentTypesReader()
  405. for k, v := range content.Overrides {
  406. if v.PartName == "/xl/"+target {
  407. content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...)
  408. }
  409. }
  410. }
  411. // CopySheet provides a function to duplicate a worksheet by gave source and
  412. // target worksheet index. Note that currently doesn't support duplicate
  413. // workbooks that contain tables, charts or pictures. For Example:
  414. //
  415. // // Sheet1 already exists...
  416. // index := f.NewSheet("Sheet2")
  417. // err := f.CopySheet(1, index)
  418. // return err
  419. //
  420. func (f *File) CopySheet(from, to int) error {
  421. if from < 1 || to < 1 || from == to || f.GetSheetName(from) == "" || f.GetSheetName(to) == "" {
  422. return errors.New("invalid worksheet index")
  423. }
  424. return f.copySheet(from, to)
  425. }
  426. // copySheet provides a function to duplicate a worksheet by gave source and
  427. // target worksheet name.
  428. func (f *File) copySheet(from, to int) error {
  429. sheet, err := f.workSheetReader("sheet" + strconv.Itoa(from))
  430. if err != nil {
  431. return err
  432. }
  433. worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet)
  434. path := "xl/worksheets/sheet" + strconv.Itoa(to) + ".xml"
  435. if len(worksheet.SheetViews.SheetView) > 0 {
  436. worksheet.SheetViews.SheetView[0].TabSelected = false
  437. }
  438. worksheet.Drawing = nil
  439. worksheet.TableParts = nil
  440. worksheet.PageSetUp = nil
  441. f.Sheet[path] = worksheet
  442. toRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(to) + ".xml.rels"
  443. fromRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(from) + ".xml.rels"
  444. _, ok := f.XLSX[fromRels]
  445. if ok {
  446. f.XLSX[toRels] = f.XLSX[fromRels]
  447. }
  448. return err
  449. }
  450. // SetSheetVisible provides a function to set worksheet visible by given worksheet
  451. // name. A workbook must contain at least one visible worksheet. If the given
  452. // worksheet has been activated, this setting will be invalidated. Sheet state
  453. // values as defined by http://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.sheetstatevalues.aspx
  454. //
  455. // visible
  456. // hidden
  457. // veryHidden
  458. //
  459. // For example, hide Sheet1:
  460. //
  461. // err := f.SetSheetVisible("Sheet1", false)
  462. //
  463. func (f *File) SetSheetVisible(name string, visible bool) error {
  464. name = trimSheetName(name)
  465. content := f.workbookReader()
  466. if visible {
  467. for k, v := range content.Sheets.Sheet {
  468. if v.Name == name {
  469. content.Sheets.Sheet[k].State = ""
  470. }
  471. }
  472. return nil
  473. }
  474. count := 0
  475. for _, v := range content.Sheets.Sheet {
  476. if v.State != "hidden" {
  477. count++
  478. }
  479. }
  480. for k, v := range content.Sheets.Sheet {
  481. xlsx, err := f.workSheetReader(f.GetSheetMap()[k])
  482. if err != nil {
  483. return err
  484. }
  485. tabSelected := false
  486. if len(xlsx.SheetViews.SheetView) > 0 {
  487. tabSelected = xlsx.SheetViews.SheetView[0].TabSelected
  488. }
  489. if v.Name == name && count > 1 && !tabSelected {
  490. content.Sheets.Sheet[k].State = "hidden"
  491. }
  492. }
  493. return nil
  494. }
  495. // parseFormatPanesSet provides a function to parse the panes settings.
  496. func parseFormatPanesSet(formatSet string) (*formatPanes, error) {
  497. format := formatPanes{}
  498. err := json.Unmarshal([]byte(formatSet), &format)
  499. return &format, err
  500. }
  501. // SetPanes provides a function to create and remove freeze panes and split panes
  502. // by given worksheet name and panes format set.
  503. //
  504. // activePane defines the pane that is active. The possible values for this
  505. // attribute are defined in the following table:
  506. //
  507. // Enumeration Value | Description
  508. // --------------------------------+-------------------------------------------------------------
  509. // bottomLeft (Bottom Left Pane) | Bottom left pane, when both vertical and horizontal
  510. // | splits are applied.
  511. // |
  512. // | This value is also used when only a horizontal split has
  513. // | been applied, dividing the pane into upper and lower
  514. // | regions. In that case, this value specifies the bottom
  515. // | pane.
  516. // |
  517. // bottomRight (Bottom Right Pane) | Bottom right pane, when both vertical and horizontal
  518. // | splits are applied.
  519. // |
  520. // topLeft (Top Left Pane) | Top left pane, when both vertical and horizontal splits
  521. // | are applied.
  522. // |
  523. // | This value is also used when only a horizontal split has
  524. // | been applied, dividing the pane into upper and lower
  525. // | regions. In that case, this value specifies the top pane.
  526. // |
  527. // | This value is also used when only a vertical split has
  528. // | been applied, dividing the pane into right and left
  529. // | regions. In that case, this value specifies the left pane
  530. // |
  531. // topRight (Top Right Pane) | Top right pane, when both vertical and horizontal
  532. // | splits are applied.
  533. // |
  534. // | This value is also used when only a vertical split has
  535. // | been applied, dividing the pane into right and left
  536. // | regions. In that case, this value specifies the right
  537. // | pane.
  538. //
  539. // Pane state type is restricted to the values supported currently listed in the following table:
  540. //
  541. // Enumeration Value | Description
  542. // --------------------------------+-------------------------------------------------------------
  543. // frozen (Frozen) | Panes are frozen, but were not split being frozen. In
  544. // | this state, when the panes are unfrozen again, a single
  545. // | pane results, with no split.
  546. // |
  547. // | In this state, the split bars are not adjustable.
  548. // |
  549. // split (Split) | Panes are split, but not frozen. In this state, the split
  550. // | bars are adjustable by the user.
  551. //
  552. // x_split (Horizontal Split Position): Horizontal position of the split, in
  553. // 1/20th of a point; 0 (zero) if none. If the pane is frozen, this value
  554. // indicates the number of columns visible in the top pane.
  555. //
  556. // y_split (Vertical Split Position): Vertical position of the split, in 1/20th
  557. // of a point; 0 (zero) if none. If the pane is frozen, this value indicates the
  558. // number of rows visible in the left pane. The possible values for this
  559. // attribute are defined by the W3C XML Schema double datatype.
  560. //
  561. // top_left_cell: Location of the top left visible cell in the bottom right pane
  562. // (when in Left-To-Right mode).
  563. //
  564. // sqref (Sequence of References): Range of the selection. Can be non-contiguous
  565. // set of ranges.
  566. //
  567. // An example of how to freeze column A in the Sheet1 and set the active cell on
  568. // Sheet1!K16:
  569. //
  570. // f.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"}]}`)
  571. //
  572. // An example of how to freeze rows 1 to 9 in the Sheet1 and set the active cell
  573. // ranges on Sheet1!A11:XFD11:
  574. //
  575. // f.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"}]}`)
  576. //
  577. // An example of how to create split panes in the Sheet1 and set the active cell
  578. // on Sheet1!J60:
  579. //
  580. // f.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"}]}`)
  581. //
  582. // An example of how to unfreeze and remove all panes on Sheet1:
  583. //
  584. // f.SetPanes("Sheet1", `{"freeze":false,"split":false}`)
  585. //
  586. func (f *File) SetPanes(sheet, panes string) error {
  587. fs, _ := parseFormatPanesSet(panes)
  588. xlsx, err := f.workSheetReader(sheet)
  589. if err != nil {
  590. return err
  591. }
  592. p := &xlsxPane{
  593. ActivePane: fs.ActivePane,
  594. TopLeftCell: fs.TopLeftCell,
  595. XSplit: float64(fs.XSplit),
  596. YSplit: float64(fs.YSplit),
  597. }
  598. if fs.Freeze {
  599. p.State = "frozen"
  600. }
  601. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Pane = p
  602. if !(fs.Freeze) && !(fs.Split) {
  603. if len(xlsx.SheetViews.SheetView) > 0 {
  604. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Pane = nil
  605. }
  606. }
  607. s := []*xlsxSelection{}
  608. for _, p := range fs.Panes {
  609. s = append(s, &xlsxSelection{
  610. ActiveCell: p.ActiveCell,
  611. Pane: p.Pane,
  612. SQRef: p.SQRef,
  613. })
  614. }
  615. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Selection = s
  616. return err
  617. }
  618. // GetSheetVisible provides a function to get worksheet visible by given worksheet
  619. // name. For example, get visible state of Sheet1:
  620. //
  621. // f.GetSheetVisible("Sheet1")
  622. //
  623. func (f *File) GetSheetVisible(name string) bool {
  624. content := f.workbookReader()
  625. visible := false
  626. for k, v := range content.Sheets.Sheet {
  627. if v.Name == trimSheetName(name) {
  628. if content.Sheets.Sheet[k].State == "" || content.Sheets.Sheet[k].State == "visible" {
  629. visible = true
  630. }
  631. }
  632. }
  633. return visible
  634. }
  635. // SearchSheet provides a function to get coordinates by given worksheet name,
  636. // cell value, and regular expression. The function doesn't support searching
  637. // on the calculated result, formatted numbers and conditional lookup
  638. // currently. If it is a merged cell, it will return the coordinates of the
  639. // upper left corner of the merged area.
  640. //
  641. // An example of search the coordinates of the value of "100" on Sheet1:
  642. //
  643. // result, err := f.SearchSheet("Sheet1", "100")
  644. //
  645. // An example of search the coordinates where the numerical value in the range
  646. // of "0-9" of Sheet1 is described:
  647. //
  648. // result, err := f.SearchSheet("Sheet1", "[0-9]", true)
  649. //
  650. func (f *File) SearchSheet(sheet, value string, reg ...bool) ([]string, error) {
  651. var (
  652. regSearch bool
  653. result []string
  654. inElement string
  655. r xlsxRow
  656. )
  657. for _, r := range reg {
  658. regSearch = r
  659. }
  660. xlsx, err := f.workSheetReader(sheet)
  661. if err != nil {
  662. return result, err
  663. }
  664. name, ok := f.sheetMap[trimSheetName(sheet)]
  665. if !ok {
  666. return result, nil
  667. }
  668. if xlsx != nil {
  669. output, _ := xml.Marshal(f.Sheet[name])
  670. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpaceBytes(output))
  671. }
  672. xml.NewDecoder(bytes.NewReader(f.readXML(name)))
  673. d := f.sharedStringsReader()
  674. decoder := xml.NewDecoder(bytes.NewReader(f.readXML(name)))
  675. for {
  676. token, _ := decoder.Token()
  677. if token == nil {
  678. break
  679. }
  680. switch startElement := token.(type) {
  681. case xml.StartElement:
  682. inElement = startElement.Name.Local
  683. if inElement == "row" {
  684. r = xlsxRow{}
  685. _ = decoder.DecodeElement(&r, &startElement)
  686. for _, colCell := range r.C {
  687. val, _ := colCell.getValueFrom(f, d)
  688. if regSearch {
  689. regex := regexp.MustCompile(value)
  690. if !regex.MatchString(val) {
  691. continue
  692. }
  693. } else {
  694. if val != value {
  695. continue
  696. }
  697. }
  698. cellCol, _, err := CellNameToCoordinates(colCell.R)
  699. if err != nil {
  700. return result, err
  701. }
  702. cellName, err := CoordinatesToCellName(cellCol, r.R)
  703. if err != nil {
  704. return result, err
  705. }
  706. result = append(result, cellName)
  707. }
  708. }
  709. default:
  710. }
  711. }
  712. return result, nil
  713. }
  714. // ProtectSheet provides a function to prevent other users from accidentally
  715. // or deliberately changing, moving, or deleting data in a worksheet. For
  716. // example, protect Sheet1 with protection settings:
  717. //
  718. // err := f.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{
  719. // Password: "password",
  720. // EditScenarios: false,
  721. // })
  722. //
  723. func (f *File) ProtectSheet(sheet string, settings *FormatSheetProtection) error {
  724. xlsx, err := f.workSheetReader(sheet)
  725. if err != nil {
  726. return err
  727. }
  728. if settings == nil {
  729. settings = &FormatSheetProtection{
  730. EditObjects: true,
  731. EditScenarios: true,
  732. SelectLockedCells: true,
  733. }
  734. }
  735. xlsx.SheetProtection = &xlsxSheetProtection{
  736. AutoFilter: settings.AutoFilter,
  737. DeleteColumns: settings.DeleteColumns,
  738. DeleteRows: settings.DeleteRows,
  739. FormatCells: settings.FormatCells,
  740. FormatColumns: settings.FormatColumns,
  741. FormatRows: settings.FormatRows,
  742. InsertColumns: settings.InsertColumns,
  743. InsertHyperlinks: settings.InsertHyperlinks,
  744. InsertRows: settings.InsertRows,
  745. Objects: settings.EditObjects,
  746. PivotTables: settings.PivotTables,
  747. Scenarios: settings.EditScenarios,
  748. SelectLockedCells: settings.SelectLockedCells,
  749. SelectUnlockedCells: settings.SelectUnlockedCells,
  750. Sheet: true,
  751. Sort: settings.Sort,
  752. }
  753. if settings.Password != "" {
  754. xlsx.SheetProtection.Password = genSheetPasswd(settings.Password)
  755. }
  756. return err
  757. }
  758. // UnprotectSheet provides a function to unprotect an Excel worksheet.
  759. func (f *File) UnprotectSheet(sheet string) error {
  760. xlsx, err := f.workSheetReader(sheet)
  761. if err != nil {
  762. return err
  763. }
  764. xlsx.SheetProtection = nil
  765. return err
  766. }
  767. // trimSheetName provides a function to trim invaild characters by given worksheet
  768. // name.
  769. func trimSheetName(name string) string {
  770. if strings.ContainsAny(name, ":\\/?*[]") || utf8.RuneCountInString(name) > 31 {
  771. r := make([]rune, 0, 31)
  772. for _, v := range name {
  773. switch v {
  774. case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[]
  775. continue
  776. default:
  777. r = append(r, v)
  778. }
  779. if len(r) == 31 {
  780. break
  781. }
  782. }
  783. name = string(r)
  784. }
  785. return name
  786. }
  787. // PageLayoutOption is an option of a page layout of a worksheet. See
  788. // SetPageLayout().
  789. type PageLayoutOption interface {
  790. setPageLayout(layout *xlsxPageSetUp)
  791. }
  792. // PageLayoutOptionPtr is a writable PageLayoutOption. See GetPageLayout().
  793. type PageLayoutOptionPtr interface {
  794. PageLayoutOption
  795. getPageLayout(layout *xlsxPageSetUp)
  796. }
  797. type (
  798. // PageLayoutOrientation defines the orientation of page layout for a
  799. // worksheet.
  800. PageLayoutOrientation string
  801. // PageLayoutPaperSize defines the paper size of the worksheet
  802. PageLayoutPaperSize int
  803. )
  804. const (
  805. // OrientationPortrait indicates page layout orientation id portrait.
  806. OrientationPortrait = "portrait"
  807. // OrientationLandscape indicates page layout orientation id landscape.
  808. OrientationLandscape = "landscape"
  809. )
  810. // setPageLayout provides a method to set the orientation for the worksheet.
  811. func (o PageLayoutOrientation) setPageLayout(ps *xlsxPageSetUp) {
  812. ps.Orientation = string(o)
  813. }
  814. // getPageLayout provides a method to get the orientation for the worksheet.
  815. func (o *PageLayoutOrientation) getPageLayout(ps *xlsxPageSetUp) {
  816. // Excel default: portrait
  817. if ps == nil || ps.Orientation == "" {
  818. *o = OrientationPortrait
  819. return
  820. }
  821. *o = PageLayoutOrientation(ps.Orientation)
  822. }
  823. // setPageLayout provides a method to set the paper size for the worksheet.
  824. func (p PageLayoutPaperSize) setPageLayout(ps *xlsxPageSetUp) {
  825. ps.PaperSize = int(p)
  826. }
  827. // getPageLayout provides a method to get the paper size for the worksheet.
  828. func (p *PageLayoutPaperSize) getPageLayout(ps *xlsxPageSetUp) {
  829. // Excel default: 1
  830. if ps == nil || ps.PaperSize == 0 {
  831. *p = 1
  832. return
  833. }
  834. *p = PageLayoutPaperSize(ps.PaperSize)
  835. }
  836. // SetPageLayout provides a function to sets worksheet page layout.
  837. //
  838. // Available options:
  839. // PageLayoutOrientation(string)
  840. // PageLayoutPaperSize(int)
  841. //
  842. // The following shows the paper size sorted by excelize index number:
  843. //
  844. // Index | Paper Size
  845. // -------+-----------------------------------------------
  846. // 1 | Letter paper (8.5 in. by 11 in.)
  847. // 2 | Letter small paper (8.5 in. by 11 in.)
  848. // 3 | Tabloid paper (11 in. by 17 in.)
  849. // 4 | Ledger paper (17 in. by 11 in.)
  850. // 5 | Legal paper (8.5 in. by 14 in.)
  851. // 6 | Statement paper (5.5 in. by 8.5 in.)
  852. // 7 | Executive paper (7.25 in. by 10.5 in.)
  853. // 8 | A3 paper (297 mm by 420 mm)
  854. // 9 | A4 paper (210 mm by 297 mm)
  855. // 10 | A4 small paper (210 mm by 297 mm)
  856. // 11 | A5 paper (148 mm by 210 mm)
  857. // 12 | B4 paper (250 mm by 353 mm)
  858. // 13 | B5 paper (176 mm by 250 mm)
  859. // 14 | Folio paper (8.5 in. by 13 in.)
  860. // 15 | Quarto paper (215 mm by 275 mm)
  861. // 16 | Standard paper (10 in. by 14 in.)
  862. // 17 | Standard paper (11 in. by 17 in.)
  863. // 18 | Note paper (8.5 in. by 11 in.)
  864. // 19 | #9 envelope (3.875 in. by 8.875 in.)
  865. // 20 | #10 envelope (4.125 in. by 9.5 in.)
  866. // 21 | #11 envelope (4.5 in. by 10.375 in.)
  867. // 22 | #12 envelope (4.75 in. by 11 in.)
  868. // 23 | #14 envelope (5 in. by 11.5 in.)
  869. // 24 | C paper (17 in. by 22 in.)
  870. // 25 | D paper (22 in. by 34 in.)
  871. // 26 | E paper (34 in. by 44 in.)
  872. // 27 | DL envelope (110 mm by 220 mm)
  873. // 28 | C5 envelope (162 mm by 229 mm)
  874. // 29 | C3 envelope (324 mm by 458 mm)
  875. // 30 | C4 envelope (229 mm by 324 mm)
  876. // 31 | C6 envelope (114 mm by 162 mm)
  877. // 32 | C65 envelope (114 mm by 229 mm)
  878. // 33 | B4 envelope (250 mm by 353 mm)
  879. // 34 | B5 envelope (176 mm by 250 mm)
  880. // 35 | B6 envelope (176 mm by 125 mm)
  881. // 36 | Italy envelope (110 mm by 230 mm)
  882. // 37 | Monarch envelope (3.875 in. by 7.5 in.).
  883. // 38 | 6 3/4 envelope (3.625 in. by 6.5 in.)
  884. // 39 | US standard fanfold (14.875 in. by 11 in.)
  885. // 40 | German standard fanfold (8.5 in. by 12 in.)
  886. // 41 | German legal fanfold (8.5 in. by 13 in.)
  887. // 42 | ISO B4 (250 mm by 353 mm)
  888. // 43 | Japanese postcard (100 mm by 148 mm)
  889. // 44 | Standard paper (9 in. by 11 in.)
  890. // 45 | Standard paper (10 in. by 11 in.)
  891. // 46 | Standard paper (15 in. by 11 in.)
  892. // 47 | Invite envelope (220 mm by 220 mm)
  893. // 50 | Letter extra paper (9.275 in. by 12 in.)
  894. // 51 | Legal extra paper (9.275 in. by 15 in.)
  895. // 52 | Tabloid extra paper (11.69 in. by 18 in.)
  896. // 53 | A4 extra paper (236 mm by 322 mm)
  897. // 54 | Letter transverse paper (8.275 in. by 11 in.)
  898. // 55 | A4 transverse paper (210 mm by 297 mm)
  899. // 56 | Letter extra transverse paper (9.275 in. by 12 in.)
  900. // 57 | SuperA/SuperA/A4 paper (227 mm by 356 mm)
  901. // 58 | SuperB/SuperB/A3 paper (305 mm by 487 mm)
  902. // 59 | Letter plus paper (8.5 in. by 12.69 in.)
  903. // 60 | A4 plus paper (210 mm by 330 mm)
  904. // 61 | A5 transverse paper (148 mm by 210 mm)
  905. // 62 | JIS B5 transverse paper (182 mm by 257 mm)
  906. // 63 | A3 extra paper (322 mm by 445 mm)
  907. // 64 | A5 extra paper (174 mm by 235 mm)
  908. // 65 | ISO B5 extra paper (201 mm by 276 mm)
  909. // 66 | A2 paper (420 mm by 594 mm)
  910. // 67 | A3 transverse paper (297 mm by 420 mm)
  911. // 68 | A3 extra transverse paper (322 mm by 445 mm)
  912. // 69 | Japanese Double Postcard (200 mm x 148 mm)
  913. // 70 | A6 (105 mm x 148 mm)
  914. // 71 | Japanese Envelope Kaku #2
  915. // 72 | Japanese Envelope Kaku #3
  916. // 73 | Japanese Envelope Chou #3
  917. // 74 | Japanese Envelope Chou #4
  918. // 75 | Letter Rotated (11in x 8 1/2 11 in)
  919. // 76 | A3 Rotated (420 mm x 297 mm)
  920. // 77 | A4 Rotated (297 mm x 210 mm)
  921. // 78 | A5 Rotated (210 mm x 148 mm)
  922. // 79 | B4 (JIS) Rotated (364 mm x 257 mm)
  923. // 80 | B5 (JIS) Rotated (257 mm x 182 mm)
  924. // 81 | Japanese Postcard Rotated (148 mm x 100 mm)
  925. // 82 | Double Japanese Postcard Rotated (148 mm x 200 mm)
  926. // 83 | A6 Rotated (148 mm x 105 mm)
  927. // 84 | Japanese Envelope Kaku #2 Rotated
  928. // 85 | Japanese Envelope Kaku #3 Rotated
  929. // 86 | Japanese Envelope Chou #3 Rotated
  930. // 87 | Japanese Envelope Chou #4 Rotated
  931. // 88 | B6 (JIS) (128 mm x 182 mm)
  932. // 89 | B6 (JIS) Rotated (182 mm x 128 mm)
  933. // 90 | (12 in x 11 in)
  934. // 91 | Japanese Envelope You #4
  935. // 92 | Japanese Envelope You #4 Rotated
  936. // 93 | PRC 16K (146 mm x 215 mm)
  937. // 94 | PRC 32K (97 mm x 151 mm)
  938. // 95 | PRC 32K(Big) (97 mm x 151 mm)
  939. // 96 | PRC Envelope #1 (102 mm x 165 mm)
  940. // 97 | PRC Envelope #2 (102 mm x 176 mm)
  941. // 98 | PRC Envelope #3 (125 mm x 176 mm)
  942. // 99 | PRC Envelope #4 (110 mm x 208 mm)
  943. // 100 | PRC Envelope #5 (110 mm x 220 mm)
  944. // 101 | PRC Envelope #6 (120 mm x 230 mm)
  945. // 102 | PRC Envelope #7 (160 mm x 230 mm)
  946. // 103 | PRC Envelope #8 (120 mm x 309 mm)
  947. // 104 | PRC Envelope #9 (229 mm x 324 mm)
  948. // 105 | PRC Envelope #10 (324 mm x 458 mm)
  949. // 106 | PRC 16K Rotated
  950. // 107 | PRC 32K Rotated
  951. // 108 | PRC 32K(Big) Rotated
  952. // 109 | PRC Envelope #1 Rotated (165 mm x 102 mm)
  953. // 110 | PRC Envelope #2 Rotated (176 mm x 102 mm)
  954. // 111 | PRC Envelope #3 Rotated (176 mm x 125 mm)
  955. // 112 | PRC Envelope #4 Rotated (208 mm x 110 mm)
  956. // 113 | PRC Envelope #5 Rotated (220 mm x 110 mm)
  957. // 114 | PRC Envelope #6 Rotated (230 mm x 120 mm)
  958. // 115 | PRC Envelope #7 Rotated (230 mm x 160 mm)
  959. // 116 | PRC Envelope #8 Rotated (309 mm x 120 mm)
  960. // 117 | PRC Envelope #9 Rotated (324 mm x 229 mm)
  961. // 118 | PRC Envelope #10 Rotated (458 mm x 324 mm)
  962. //
  963. func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error {
  964. s, err := f.workSheetReader(sheet)
  965. if err != nil {
  966. return err
  967. }
  968. ps := s.PageSetUp
  969. if ps == nil {
  970. ps = new(xlsxPageSetUp)
  971. s.PageSetUp = ps
  972. }
  973. for _, opt := range opts {
  974. opt.setPageLayout(ps)
  975. }
  976. return err
  977. }
  978. // GetPageLayout provides a function to gets worksheet page layout.
  979. //
  980. // Available options:
  981. // PageLayoutOrientation(string)
  982. // PageLayoutPaperSize(int)
  983. func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error {
  984. s, err := f.workSheetReader(sheet)
  985. if err != nil {
  986. return err
  987. }
  988. ps := s.PageSetUp
  989. for _, opt := range opts {
  990. opt.getPageLayout(ps)
  991. }
  992. return err
  993. }
  994. // workSheetRelsReader provides a function to get the pointer to the structure
  995. // after deserialization of xl/worksheets/_rels/sheet%d.xml.rels.
  996. func (f *File) workSheetRelsReader(path string) *xlsxWorkbookRels {
  997. if f.WorkSheetRels[path] == nil {
  998. _, ok := f.XLSX[path]
  999. if ok {
  1000. c := xlsxWorkbookRels{}
  1001. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(path)), &c)
  1002. f.WorkSheetRels[path] = &c
  1003. }
  1004. }
  1005. return f.WorkSheetRels[path]
  1006. }
  1007. // workSheetRelsWriter provides a function to save
  1008. // xl/worksheets/_rels/sheet%d.xml.rels after serialize structure.
  1009. func (f *File) workSheetRelsWriter() {
  1010. for p, r := range f.WorkSheetRels {
  1011. if r != nil {
  1012. v, _ := xml.Marshal(r)
  1013. f.saveFileList(p, v)
  1014. }
  1015. }
  1016. }
  1017. // fillSheetData ensures there are enough rows, and columns in the chosen
  1018. // row to accept data. Missing rows are backfilled and given their row number
  1019. func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) {
  1020. rowCount := len(xlsx.SheetData.Row)
  1021. if rowCount < row {
  1022. // append missing rows
  1023. for rowIdx := rowCount; rowIdx < row; rowIdx++ {
  1024. xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1})
  1025. }
  1026. }
  1027. rowData := &xlsx.SheetData.Row[row-1]
  1028. fillColumns(rowData, col, row)
  1029. }
  1030. func fillColumns(rowData *xlsxRow, col, row int) {
  1031. cellCount := len(rowData.C)
  1032. if cellCount < col {
  1033. for colIdx := cellCount; colIdx < col; colIdx++ {
  1034. cellName, _ := CoordinatesToCellName(colIdx+1, row)
  1035. rowData.C = append(rowData.C, xlsxC{R: cellName})
  1036. }
  1037. }
  1038. }
  1039. func makeContiguousColumns(xlsx *xlsxWorksheet, fromRow, toRow, colCount int) {
  1040. for ; fromRow < toRow; fromRow++ {
  1041. rowData := &xlsx.SheetData.Row[fromRow-1]
  1042. fillColumns(rowData, colCount, fromRow)
  1043. }
  1044. }