sheet.go 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. // Copyright 2016 - 2020 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 / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.10 or later.
  11. package excelize
  12. import (
  13. "bytes"
  14. "encoding/json"
  15. "encoding/xml"
  16. "errors"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "log"
  21. "os"
  22. "path"
  23. "reflect"
  24. "regexp"
  25. "strconv"
  26. "strings"
  27. "unicode/utf8"
  28. "github.com/mohae/deepcopy"
  29. )
  30. // NewSheet provides the function to create a new sheet by given a worksheet
  31. // name and returns the index of the sheets in the workbook
  32. // (spreadsheet) after it appended. Note that when creating a new spreadsheet
  33. // file, the default worksheet named `Sheet1` will be created.
  34. func (f *File) NewSheet(name string) int {
  35. // Check if the worksheet already exists
  36. index := f.GetSheetIndex(name)
  37. if index != -1 {
  38. return index
  39. }
  40. f.DeleteSheet(name)
  41. f.SheetCount++
  42. wb := f.workbookReader()
  43. sheetID := 0
  44. for _, v := range wb.Sheets.Sheet {
  45. if v.SheetID > sheetID {
  46. sheetID = v.SheetID
  47. }
  48. }
  49. sheetID++
  50. // Update docProps/app.xml
  51. f.setAppXML()
  52. // Update [Content_Types].xml
  53. f.setContentTypes("/xl/worksheets/sheet"+strconv.Itoa(sheetID)+".xml", ContentTypeSpreadSheetMLWorksheet)
  54. // Create new sheet /xl/worksheets/sheet%d.xml
  55. f.setSheet(sheetID, name)
  56. // Update xl/_rels/workbook.xml.rels
  57. rID := f.addRels("xl/_rels/workbook.xml.rels", SourceRelationshipWorkSheet, fmt.Sprintf("worksheets/sheet%d.xml", sheetID), "")
  58. // Update xl/workbook.xml
  59. f.setWorkbook(name, sheetID, rID)
  60. return f.GetSheetIndex(name)
  61. }
  62. // contentTypesReader provides a function to get the pointer to the
  63. // [Content_Types].xml structure after deserialization.
  64. func (f *File) contentTypesReader() *xlsxTypes {
  65. var err error
  66. if f.ContentTypes == nil {
  67. f.ContentTypes = new(xlsxTypes)
  68. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("[Content_Types].xml")))).
  69. Decode(f.ContentTypes); err != nil && err != io.EOF {
  70. log.Printf("xml decode error: %s", err)
  71. }
  72. }
  73. return f.ContentTypes
  74. }
  75. // contentTypesWriter provides a function to save [Content_Types].xml after
  76. // serialize structure.
  77. func (f *File) contentTypesWriter() {
  78. if f.ContentTypes != nil {
  79. output, _ := xml.Marshal(f.ContentTypes)
  80. f.saveFileList("[Content_Types].xml", output)
  81. }
  82. }
  83. // workbookReader provides a function to get the pointer to the xl/workbook.xml
  84. // structure after deserialization.
  85. func (f *File) workbookReader() *xlsxWorkbook {
  86. var err error
  87. if f.WorkBook == nil {
  88. f.WorkBook = new(xlsxWorkbook)
  89. if _, ok := f.xmlAttr["xl/workbook.xml"]; !ok {
  90. d := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("xl/workbook.xml"))))
  91. f.xmlAttr["xl/workbook.xml"] = append(f.xmlAttr["xl/workbook.xml"], getRootElement(d)...)
  92. f.addNameSpaces("xl/workbook.xml", SourceRelationship)
  93. }
  94. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("xl/workbook.xml")))).
  95. Decode(f.WorkBook); err != nil && err != io.EOF {
  96. log.Printf("xml decode error: %s", err)
  97. }
  98. }
  99. return f.WorkBook
  100. }
  101. // workBookWriter provides a function to save xl/workbook.xml after serialize
  102. // structure.
  103. func (f *File) workBookWriter() {
  104. if f.WorkBook != nil {
  105. output, _ := xml.Marshal(f.WorkBook)
  106. f.saveFileList("xl/workbook.xml", replaceRelationshipsBytes(f.replaceNameSpaceBytes("xl/workbook.xml", output)))
  107. }
  108. }
  109. // workSheetWriter provides a function to save xl/worksheets/sheet%d.xml after
  110. // serialize structure.
  111. func (f *File) workSheetWriter() {
  112. for p, sheet := range f.Sheet {
  113. if sheet != nil {
  114. for k, v := range sheet.SheetData.Row {
  115. f.Sheet[p].SheetData.Row[k].C = trimCell(v.C)
  116. }
  117. output, _ := xml.Marshal(sheet)
  118. f.saveFileList(p, replaceRelationshipsBytes(f.replaceNameSpaceBytes(p, output)))
  119. ok := f.checked[p]
  120. if ok {
  121. delete(f.Sheet, p)
  122. f.checked[p] = false
  123. }
  124. }
  125. }
  126. }
  127. // trimCell provides a function to trim blank cells which created by fillColumns.
  128. func trimCell(column []xlsxC) []xlsxC {
  129. rowFull := true
  130. for i := range column {
  131. rowFull = column[i].hasValue() && rowFull
  132. }
  133. if rowFull {
  134. return column
  135. }
  136. col := make([]xlsxC, len(column))
  137. i := 0
  138. for _, c := range column {
  139. if c.hasValue() {
  140. col[i] = c
  141. i++
  142. }
  143. }
  144. return col[0:i]
  145. }
  146. // setContentTypes provides a function to read and update property of contents
  147. // type of the spreadsheet.
  148. func (f *File) setContentTypes(partName, contentType string) {
  149. content := f.contentTypesReader()
  150. content.Overrides = append(content.Overrides, xlsxOverride{
  151. PartName: partName,
  152. ContentType: contentType,
  153. })
  154. }
  155. // setSheet provides a function to update sheet property by given index.
  156. func (f *File) setSheet(index int, name string) {
  157. xlsx := xlsxWorksheet{
  158. Dimension: &xlsxDimension{Ref: "A1"},
  159. SheetViews: &xlsxSheetViews{
  160. SheetView: []xlsxSheetView{{WorkbookViewID: 0}},
  161. },
  162. }
  163. path := "xl/worksheets/sheet" + strconv.Itoa(index) + ".xml"
  164. f.sheetMap[trimSheetName(name)] = path
  165. f.Sheet[path] = &xlsx
  166. f.xmlAttr[path] = append(f.xmlAttr[path], NameSpaceSpreadSheet)
  167. }
  168. // setWorkbook update workbook property of the spreadsheet. Maximum 31
  169. // characters are allowed in sheet title.
  170. func (f *File) setWorkbook(name string, sheetID, rid int) {
  171. content := f.workbookReader()
  172. content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{
  173. Name: trimSheetName(name),
  174. SheetID: sheetID,
  175. ID: "rId" + strconv.Itoa(rid),
  176. })
  177. }
  178. // relsWriter provides a function to save relationships after
  179. // serialize structure.
  180. func (f *File) relsWriter() {
  181. for path, rel := range f.Relationships {
  182. if rel != nil {
  183. output, _ := xml.Marshal(rel)
  184. if strings.HasPrefix(path, "xl/worksheets/sheet/rels/sheet") {
  185. output = f.replaceNameSpaceBytes(path, output)
  186. }
  187. f.saveFileList(path, replaceRelationshipsBytes(output))
  188. }
  189. }
  190. }
  191. // setAppXML update docProps/app.xml file of XML.
  192. func (f *File) setAppXML() {
  193. f.saveFileList("docProps/app.xml", []byte(templateDocpropsApp))
  194. }
  195. // replaceRelationshipsBytes; Some tools that read spreadsheet files have very
  196. // strict requirements about the structure of the input XML. This function is
  197. // a horrible hack to fix that after the XML marshalling is completed.
  198. func replaceRelationshipsBytes(content []byte) []byte {
  199. oldXmlns := []byte(`xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships`)
  200. newXmlns := []byte("r")
  201. return bytesReplace(content, oldXmlns, newXmlns, -1)
  202. }
  203. // SetActiveSheet provides a function to set the default active sheet of the
  204. // workbook by a given index. Note that the active index is different from the
  205. // ID returned by function GetSheetMap(). It should be greater or equal to 0
  206. // and less than the total worksheet numbers.
  207. func (f *File) SetActiveSheet(index int) {
  208. if index < 0 {
  209. index = 0
  210. }
  211. wb := f.workbookReader()
  212. for activeTab := range wb.Sheets.Sheet {
  213. if activeTab == index {
  214. if wb.BookViews == nil {
  215. wb.BookViews = &xlsxBookViews{}
  216. }
  217. if len(wb.BookViews.WorkBookView) > 0 {
  218. wb.BookViews.WorkBookView[0].ActiveTab = activeTab
  219. } else {
  220. wb.BookViews.WorkBookView = append(wb.BookViews.WorkBookView, xlsxWorkBookView{
  221. ActiveTab: activeTab,
  222. })
  223. }
  224. }
  225. }
  226. for idx, name := range f.GetSheetList() {
  227. xlsx, err := f.workSheetReader(name)
  228. if err != nil {
  229. // Chartsheet or dialogsheet
  230. return
  231. }
  232. if xlsx.SheetViews == nil {
  233. xlsx.SheetViews = &xlsxSheetViews{
  234. SheetView: []xlsxSheetView{{WorkbookViewID: 0}},
  235. }
  236. }
  237. if len(xlsx.SheetViews.SheetView) > 0 {
  238. xlsx.SheetViews.SheetView[0].TabSelected = false
  239. }
  240. if index == idx {
  241. if len(xlsx.SheetViews.SheetView) > 0 {
  242. xlsx.SheetViews.SheetView[0].TabSelected = true
  243. } else {
  244. xlsx.SheetViews.SheetView = append(xlsx.SheetViews.SheetView, xlsxSheetView{
  245. TabSelected: true,
  246. })
  247. }
  248. }
  249. }
  250. }
  251. // GetActiveSheetIndex provides a function to get active sheet index of the
  252. // spreadsheet. If not found the active sheet will be return integer 0.
  253. func (f *File) GetActiveSheetIndex() (index int) {
  254. var sheetID = f.getActiveSheetID()
  255. wb := f.workbookReader()
  256. if wb != nil {
  257. for idx, sheet := range wb.Sheets.Sheet {
  258. if sheet.SheetID == sheetID {
  259. index = idx
  260. }
  261. }
  262. }
  263. return
  264. }
  265. // getActiveSheetID provides a function to get active sheet index of the
  266. // spreadsheet. If not found the active sheet will be return integer 0.
  267. func (f *File) getActiveSheetID() int {
  268. wb := f.workbookReader()
  269. if wb != nil {
  270. if wb.BookViews != nil && len(wb.BookViews.WorkBookView) > 0 {
  271. activeTab := wb.BookViews.WorkBookView[0].ActiveTab
  272. if len(wb.Sheets.Sheet) > activeTab && wb.Sheets.Sheet[activeTab].SheetID != 0 {
  273. return wb.Sheets.Sheet[activeTab].SheetID
  274. }
  275. }
  276. if len(wb.Sheets.Sheet) >= 1 {
  277. return wb.Sheets.Sheet[0].SheetID
  278. }
  279. }
  280. return 0
  281. }
  282. // SetSheetName provides a function to set the worksheet name by given old and
  283. // new worksheet names. Maximum 31 characters are allowed in sheet title and
  284. // this function only changes the name of the sheet and will not update the
  285. // sheet name in the formula or reference associated with the cell. So there
  286. // may be problem formula error or reference missing.
  287. func (f *File) SetSheetName(oldName, newName string) {
  288. oldName = trimSheetName(oldName)
  289. newName = trimSheetName(newName)
  290. if newName == oldName {
  291. return
  292. }
  293. content := f.workbookReader()
  294. for k, v := range content.Sheets.Sheet {
  295. if v.Name == oldName {
  296. content.Sheets.Sheet[k].Name = newName
  297. f.sheetMap[newName] = f.sheetMap[oldName]
  298. delete(f.sheetMap, oldName)
  299. }
  300. }
  301. }
  302. // getSheetNameByID provides a function to get worksheet name of the
  303. // spreadsheet by given worksheet ID. If given sheet ID is invalid, will
  304. // return an empty string.
  305. func (f *File) getSheetNameByID(ID int) string {
  306. wb := f.workbookReader()
  307. if wb == nil || ID < 1 {
  308. return ""
  309. }
  310. for _, sheet := range wb.Sheets.Sheet {
  311. if ID == sheet.SheetID {
  312. return sheet.Name
  313. }
  314. }
  315. return ""
  316. }
  317. // GetSheetName provides a function to get the sheet name of the workbook by
  318. // the given sheet index. If the given sheet index is invalid, it will return
  319. // an empty string.
  320. func (f *File) GetSheetName(index int) (name string) {
  321. for idx, sheet := range f.GetSheetList() {
  322. if idx == index {
  323. name = sheet
  324. }
  325. }
  326. return
  327. }
  328. // getSheetID provides a function to get worksheet ID of the spreadsheet by
  329. // given sheet name. If given worksheet name is invalid, will return an
  330. // integer type value -1.
  331. func (f *File) getSheetID(name string) int {
  332. var ID = -1
  333. for sheetID, sheet := range f.GetSheetMap() {
  334. if sheet == trimSheetName(name) {
  335. ID = sheetID
  336. }
  337. }
  338. return ID
  339. }
  340. // GetSheetIndex provides a function to get a sheet index of the workbook by
  341. // the given sheet name. If the given sheet name is invalid or sheet doesn't
  342. // exist, it will return an integer type value -1.
  343. func (f *File) GetSheetIndex(name string) int {
  344. var idx = -1
  345. for index, sheet := range f.GetSheetList() {
  346. if sheet == trimSheetName(name) {
  347. idx = index
  348. }
  349. }
  350. return idx
  351. }
  352. // GetSheetMap provides a function to get worksheets, chart sheets, dialog
  353. // sheets ID and name map of the workbook. For example:
  354. //
  355. // f, err := excelize.OpenFile("Book1.xlsx")
  356. // if err != nil {
  357. // return
  358. // }
  359. // for index, name := range f.GetSheetMap() {
  360. // fmt.Println(index, name)
  361. // }
  362. //
  363. func (f *File) GetSheetMap() map[int]string {
  364. wb := f.workbookReader()
  365. sheetMap := map[int]string{}
  366. if wb != nil {
  367. for _, sheet := range wb.Sheets.Sheet {
  368. sheetMap[sheet.SheetID] = sheet.Name
  369. }
  370. }
  371. return sheetMap
  372. }
  373. // GetSheetList provides a function to get worksheets, chart sheets, and
  374. // dialog sheets name list of the workbook.
  375. func (f *File) GetSheetList() (list []string) {
  376. wb := f.workbookReader()
  377. if wb != nil {
  378. for _, sheet := range wb.Sheets.Sheet {
  379. list = append(list, sheet.Name)
  380. }
  381. }
  382. return
  383. }
  384. // getSheetMap provides a function to get worksheet name and XML file path map
  385. // of XLSX.
  386. func (f *File) getSheetMap() map[string]string {
  387. content := f.workbookReader()
  388. rels := f.relsReader("xl/_rels/workbook.xml.rels")
  389. maps := map[string]string{}
  390. for _, v := range content.Sheets.Sheet {
  391. for _, rel := range rels.Relationships {
  392. if rel.ID == v.ID {
  393. // Construct a target XML as xl/worksheets/sheet%d by split path, compatible with different types of relative paths in workbook.xml.rels, for example: worksheets/sheet%d.xml and /xl/worksheets/sheet%d.xml
  394. pathInfo := strings.Split(rel.Target, "/")
  395. pathInfoLen := len(pathInfo)
  396. if pathInfoLen > 1 {
  397. maps[v.Name] = fmt.Sprintf("xl/%s", strings.Join(pathInfo[pathInfoLen-2:], "/"))
  398. }
  399. }
  400. }
  401. }
  402. return maps
  403. }
  404. // SetSheetBackground provides a function to set background picture by given
  405. // worksheet name and file path.
  406. func (f *File) SetSheetBackground(sheet, picture string) error {
  407. var err error
  408. // Check picture exists first.
  409. if _, err = os.Stat(picture); os.IsNotExist(err) {
  410. return err
  411. }
  412. ext, ok := supportImageTypes[path.Ext(picture)]
  413. if !ok {
  414. return errors.New("unsupported image extension")
  415. }
  416. file, _ := ioutil.ReadFile(picture)
  417. name := f.addMedia(file, ext)
  418. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
  419. rID := f.addRels(sheetRels, SourceRelationshipImage, strings.Replace(name, "xl", "..", 1), "")
  420. f.addSheetPicture(sheet, rID)
  421. f.addSheetNameSpace(sheet, SourceRelationship)
  422. f.setContentTypePartImageExtensions()
  423. return err
  424. }
  425. // DeleteSheet provides a function to delete worksheet in a workbook by given
  426. // worksheet name. Use this method with caution, which will affect changes in
  427. // references such as formulas, charts, and so on. If there is any referenced
  428. // value of the deleted worksheet, it will cause a file error when you open it.
  429. // This function will be invalid when only the one worksheet is left.
  430. func (f *File) DeleteSheet(name string) {
  431. if f.SheetCount == 1 || f.GetSheetIndex(name) == -1 {
  432. return
  433. }
  434. sheetName := trimSheetName(name)
  435. wb := f.workbookReader()
  436. wbRels := f.relsReader("xl/_rels/workbook.xml.rels")
  437. for idx, sheet := range wb.Sheets.Sheet {
  438. if sheet.Name == sheetName {
  439. wb.Sheets.Sheet = append(wb.Sheets.Sheet[:idx], wb.Sheets.Sheet[idx+1:]...)
  440. var sheetXML, rels string
  441. if wbRels != nil {
  442. for _, rel := range wbRels.Relationships {
  443. if rel.ID == sheet.ID {
  444. sheetXML = fmt.Sprintf("xl/%s", rel.Target)
  445. pathInfo := strings.Split(rel.Target, "/")
  446. if len(pathInfo) == 2 {
  447. rels = fmt.Sprintf("xl/%s/_rels/%s.rels", pathInfo[0], pathInfo[1])
  448. }
  449. }
  450. }
  451. }
  452. target := f.deleteSheetFromWorkbookRels(sheet.ID)
  453. f.deleteSheetFromContentTypes(target)
  454. f.deleteCalcChain(sheet.SheetID, "") // Delete CalcChain
  455. delete(f.sheetMap, sheetName)
  456. delete(f.XLSX, sheetXML)
  457. delete(f.XLSX, rels)
  458. delete(f.Relationships, rels)
  459. delete(f.Sheet, sheetXML)
  460. delete(f.xmlAttr, sheetXML)
  461. f.SheetCount--
  462. }
  463. }
  464. if wb.BookViews != nil {
  465. for idx, bookView := range wb.BookViews.WorkBookView {
  466. if bookView.ActiveTab >= f.SheetCount {
  467. wb.BookViews.WorkBookView[idx].ActiveTab--
  468. }
  469. }
  470. }
  471. f.SetActiveSheet(len(f.GetSheetMap()))
  472. }
  473. // deleteSheetFromWorkbookRels provides a function to remove worksheet
  474. // relationships by given relationships ID in the file
  475. // xl/_rels/workbook.xml.rels.
  476. func (f *File) deleteSheetFromWorkbookRels(rID string) string {
  477. content := f.relsReader("xl/_rels/workbook.xml.rels")
  478. for k, v := range content.Relationships {
  479. if v.ID == rID {
  480. content.Relationships = append(content.Relationships[:k], content.Relationships[k+1:]...)
  481. return v.Target
  482. }
  483. }
  484. return ""
  485. }
  486. // deleteSheetFromContentTypes provides a function to remove worksheet
  487. // relationships by given target name in the file [Content_Types].xml.
  488. func (f *File) deleteSheetFromContentTypes(target string) {
  489. content := f.contentTypesReader()
  490. for k, v := range content.Overrides {
  491. if v.PartName == "/xl/"+target {
  492. content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...)
  493. }
  494. }
  495. }
  496. // CopySheet provides a function to duplicate a worksheet by gave source and
  497. // target worksheet index. Note that currently doesn't support duplicate
  498. // workbooks that contain tables, charts or pictures. For Example:
  499. //
  500. // // Sheet1 already exists...
  501. // index := f.NewSheet("Sheet2")
  502. // err := f.CopySheet(1, index)
  503. // return err
  504. //
  505. func (f *File) CopySheet(from, to int) error {
  506. if from < 0 || to < 0 || from == to || f.GetSheetName(from) == "" || f.GetSheetName(to) == "" {
  507. return errors.New("invalid worksheet index")
  508. }
  509. return f.copySheet(from, to)
  510. }
  511. // copySheet provides a function to duplicate a worksheet by gave source and
  512. // target worksheet name.
  513. func (f *File) copySheet(from, to int) error {
  514. fromSheet := f.GetSheetName(from)
  515. sheet, err := f.workSheetReader(fromSheet)
  516. if err != nil {
  517. return err
  518. }
  519. worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet)
  520. toSheetID := strconv.Itoa(f.getSheetID(f.GetSheetName(to)))
  521. path := "xl/worksheets/sheet" + toSheetID + ".xml"
  522. if len(worksheet.SheetViews.SheetView) > 0 {
  523. worksheet.SheetViews.SheetView[0].TabSelected = false
  524. }
  525. worksheet.Drawing = nil
  526. worksheet.TableParts = nil
  527. worksheet.PageSetUp = nil
  528. f.Sheet[path] = worksheet
  529. toRels := "xl/worksheets/_rels/sheet" + toSheetID + ".xml.rels"
  530. fromRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(f.getSheetID(fromSheet)) + ".xml.rels"
  531. _, ok := f.XLSX[fromRels]
  532. if ok {
  533. f.XLSX[toRels] = f.XLSX[fromRels]
  534. }
  535. fromSheetXMLPath, _ := f.sheetMap[trimSheetName(fromSheet)]
  536. fromSheetAttr, _ := f.xmlAttr[fromSheetXMLPath]
  537. f.xmlAttr[path] = fromSheetAttr
  538. return err
  539. }
  540. // SetSheetVisible provides a function to set worksheet visible by given worksheet
  541. // name. A workbook must contain at least one visible worksheet. If the given
  542. // worksheet has been activated, this setting will be invalidated. Sheet state
  543. // values as defined by https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.sheetstatevalues
  544. //
  545. // visible
  546. // hidden
  547. // veryHidden
  548. //
  549. // For example, hide Sheet1:
  550. //
  551. // err := f.SetSheetVisible("Sheet1", false)
  552. //
  553. func (f *File) SetSheetVisible(name string, visible bool) error {
  554. name = trimSheetName(name)
  555. content := f.workbookReader()
  556. if visible {
  557. for k, v := range content.Sheets.Sheet {
  558. if v.Name == name {
  559. content.Sheets.Sheet[k].State = ""
  560. }
  561. }
  562. return nil
  563. }
  564. count := 0
  565. for _, v := range content.Sheets.Sheet {
  566. if v.State != "hidden" {
  567. count++
  568. }
  569. }
  570. for k, v := range content.Sheets.Sheet {
  571. xlsx, err := f.workSheetReader(v.Name)
  572. if err != nil {
  573. return err
  574. }
  575. tabSelected := false
  576. if len(xlsx.SheetViews.SheetView) > 0 {
  577. tabSelected = xlsx.SheetViews.SheetView[0].TabSelected
  578. }
  579. if v.Name == name && count > 1 && !tabSelected {
  580. content.Sheets.Sheet[k].State = "hidden"
  581. }
  582. }
  583. return nil
  584. }
  585. // parseFormatPanesSet provides a function to parse the panes settings.
  586. func parseFormatPanesSet(formatSet string) (*formatPanes, error) {
  587. format := formatPanes{}
  588. err := json.Unmarshal([]byte(formatSet), &format)
  589. return &format, err
  590. }
  591. // SetPanes provides a function to create and remove freeze panes and split panes
  592. // by given worksheet name and panes format set.
  593. //
  594. // activePane defines the pane that is active. The possible values for this
  595. // attribute are defined in the following table:
  596. //
  597. // Enumeration Value | Description
  598. // --------------------------------+-------------------------------------------------------------
  599. // bottomLeft (Bottom Left Pane) | Bottom left pane, when both vertical and horizontal
  600. // | splits are applied.
  601. // |
  602. // | This value is also used when only a horizontal split has
  603. // | been applied, dividing the pane into upper and lower
  604. // | regions. In that case, this value specifies the bottom
  605. // | pane.
  606. // |
  607. // bottomRight (Bottom Right Pane) | Bottom right pane, when both vertical and horizontal
  608. // | splits are applied.
  609. // |
  610. // topLeft (Top Left Pane) | Top left pane, when both vertical and horizontal splits
  611. // | are applied.
  612. // |
  613. // | This value is also used when only a horizontal split has
  614. // | been applied, dividing the pane into upper and lower
  615. // | regions. In that case, this value specifies the top pane.
  616. // |
  617. // | This value is also used when only a vertical split has
  618. // | been applied, dividing the pane into right and left
  619. // | regions. In that case, this value specifies the left pane
  620. // |
  621. // topRight (Top Right Pane) | Top right pane, when both vertical and horizontal
  622. // | splits are applied.
  623. // |
  624. // | This value is also used when only a vertical split has
  625. // | been applied, dividing the pane into right and left
  626. // | regions. In that case, this value specifies the right
  627. // | pane.
  628. //
  629. // Pane state type is restricted to the values supported currently listed in the following table:
  630. //
  631. // Enumeration Value | Description
  632. // --------------------------------+-------------------------------------------------------------
  633. // frozen (Frozen) | Panes are frozen, but were not split being frozen. In
  634. // | this state, when the panes are unfrozen again, a single
  635. // | pane results, with no split.
  636. // |
  637. // | In this state, the split bars are not adjustable.
  638. // |
  639. // split (Split) | Panes are split, but not frozen. In this state, the split
  640. // | bars are adjustable by the user.
  641. //
  642. // x_split (Horizontal Split Position): Horizontal position of the split, in
  643. // 1/20th of a point; 0 (zero) if none. If the pane is frozen, this value
  644. // indicates the number of columns visible in the top pane.
  645. //
  646. // y_split (Vertical Split Position): Vertical position of the split, in 1/20th
  647. // of a point; 0 (zero) if none. If the pane is frozen, this value indicates the
  648. // number of rows visible in the left pane. The possible values for this
  649. // attribute are defined by the W3C XML Schema double datatype.
  650. //
  651. // top_left_cell: Location of the top left visible cell in the bottom right pane
  652. // (when in Left-To-Right mode).
  653. //
  654. // sqref (Sequence of References): Range of the selection. Can be non-contiguous
  655. // set of ranges.
  656. //
  657. // An example of how to freeze column A in the Sheet1 and set the active cell on
  658. // Sheet1!K16:
  659. //
  660. // 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"}]}`)
  661. //
  662. // An example of how to freeze rows 1 to 9 in the Sheet1 and set the active cell
  663. // ranges on Sheet1!A11:XFD11:
  664. //
  665. // 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"}]}`)
  666. //
  667. // An example of how to create split panes in the Sheet1 and set the active cell
  668. // on Sheet1!J60:
  669. //
  670. // 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"}]}`)
  671. //
  672. // An example of how to unfreeze and remove all panes on Sheet1:
  673. //
  674. // f.SetPanes("Sheet1", `{"freeze":false,"split":false}`)
  675. //
  676. func (f *File) SetPanes(sheet, panes string) error {
  677. fs, _ := parseFormatPanesSet(panes)
  678. xlsx, err := f.workSheetReader(sheet)
  679. if err != nil {
  680. return err
  681. }
  682. p := &xlsxPane{
  683. ActivePane: fs.ActivePane,
  684. TopLeftCell: fs.TopLeftCell,
  685. XSplit: float64(fs.XSplit),
  686. YSplit: float64(fs.YSplit),
  687. }
  688. if fs.Freeze {
  689. p.State = "frozen"
  690. }
  691. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Pane = p
  692. if !(fs.Freeze) && !(fs.Split) {
  693. if len(xlsx.SheetViews.SheetView) > 0 {
  694. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Pane = nil
  695. }
  696. }
  697. s := []*xlsxSelection{}
  698. for _, p := range fs.Panes {
  699. s = append(s, &xlsxSelection{
  700. ActiveCell: p.ActiveCell,
  701. Pane: p.Pane,
  702. SQRef: p.SQRef,
  703. })
  704. }
  705. xlsx.SheetViews.SheetView[len(xlsx.SheetViews.SheetView)-1].Selection = s
  706. return err
  707. }
  708. // GetSheetVisible provides a function to get worksheet visible by given worksheet
  709. // name. For example, get visible state of Sheet1:
  710. //
  711. // f.GetSheetVisible("Sheet1")
  712. //
  713. func (f *File) GetSheetVisible(name string) bool {
  714. content := f.workbookReader()
  715. visible := false
  716. for k, v := range content.Sheets.Sheet {
  717. if v.Name == trimSheetName(name) {
  718. if content.Sheets.Sheet[k].State == "" || content.Sheets.Sheet[k].State == "visible" {
  719. visible = true
  720. }
  721. }
  722. }
  723. return visible
  724. }
  725. // SearchSheet provides a function to get coordinates by given worksheet name,
  726. // cell value, and regular expression. The function doesn't support searching
  727. // on the calculated result, formatted numbers and conditional lookup
  728. // currently. If it is a merged cell, it will return the coordinates of the
  729. // upper left corner of the merged area.
  730. //
  731. // An example of search the coordinates of the value of "100" on Sheet1:
  732. //
  733. // result, err := f.SearchSheet("Sheet1", "100")
  734. //
  735. // An example of search the coordinates where the numerical value in the range
  736. // of "0-9" of Sheet1 is described:
  737. //
  738. // result, err := f.SearchSheet("Sheet1", "[0-9]", true)
  739. //
  740. func (f *File) SearchSheet(sheet, value string, reg ...bool) ([]string, error) {
  741. var (
  742. regSearch bool
  743. result []string
  744. )
  745. for _, r := range reg {
  746. regSearch = r
  747. }
  748. name, ok := f.sheetMap[trimSheetName(sheet)]
  749. if !ok {
  750. return result, ErrSheetNotExist{sheet}
  751. }
  752. if f.Sheet[name] != nil {
  753. // flush data
  754. output, _ := xml.Marshal(f.Sheet[name])
  755. f.saveFileList(name, f.replaceNameSpaceBytes(name, output))
  756. }
  757. return f.searchSheet(name, value, regSearch)
  758. }
  759. // searchSheet provides a function to get coordinates by given worksheet name,
  760. // cell value, and regular expression.
  761. func (f *File) searchSheet(name, value string, regSearch bool) (result []string, err error) {
  762. var (
  763. cellName, inElement string
  764. cellCol, row int
  765. d *xlsxSST
  766. )
  767. d = f.sharedStringsReader()
  768. decoder := f.xmlNewDecoder(bytes.NewReader(f.readXML(name)))
  769. for {
  770. var token xml.Token
  771. token, err = decoder.Token()
  772. if err != nil || token == nil {
  773. if err == io.EOF {
  774. err = nil
  775. }
  776. break
  777. }
  778. switch startElement := token.(type) {
  779. case xml.StartElement:
  780. inElement = startElement.Name.Local
  781. if inElement == "row" {
  782. row, err = attrValToInt("r", startElement.Attr)
  783. if err != nil {
  784. return
  785. }
  786. }
  787. if inElement == "c" {
  788. colCell := xlsxC{}
  789. _ = decoder.DecodeElement(&colCell, &startElement)
  790. val, _ := colCell.getValueFrom(f, d)
  791. if regSearch {
  792. regex := regexp.MustCompile(value)
  793. if !regex.MatchString(val) {
  794. continue
  795. }
  796. } else {
  797. if val != value {
  798. continue
  799. }
  800. }
  801. cellCol, _, err = CellNameToCoordinates(colCell.R)
  802. if err != nil {
  803. return result, err
  804. }
  805. cellName, err = CoordinatesToCellName(cellCol, row)
  806. if err != nil {
  807. return result, err
  808. }
  809. result = append(result, cellName)
  810. }
  811. default:
  812. }
  813. }
  814. return
  815. }
  816. // attrValToInt provides a function to convert the local names to an integer
  817. // by given XML attributes and specified names.
  818. func attrValToInt(name string, attrs []xml.Attr) (val int, err error) {
  819. for _, attr := range attrs {
  820. if attr.Name.Local == name {
  821. val, err = strconv.Atoi(attr.Value)
  822. if err != nil {
  823. return
  824. }
  825. }
  826. }
  827. return
  828. }
  829. // SetHeaderFooter provides a function to set headers and footers by given
  830. // worksheet name and the control characters.
  831. //
  832. // Headers and footers are specified using the following settings fields:
  833. //
  834. // Fields | Description
  835. // ------------------+-----------------------------------------------------------
  836. // AlignWithMargins | Align header footer margins with page margins
  837. // DifferentFirst | Different first-page header and footer indicator
  838. // DifferentOddEven | Different odd and even page headers and footers indicator
  839. // ScaleWithDoc | Scale header and footer with document scaling
  840. // OddFooter | Odd Page Footer
  841. // OddHeader | Odd Header
  842. // EvenFooter | Even Page Footer
  843. // EvenHeader | Even Page Header
  844. // FirstFooter | First Page Footer
  845. // FirstHeader | First Page Header
  846. //
  847. // The following formatting codes can be used in 6 string type fields:
  848. // OddHeader, OddFooter, EvenHeader, EvenFooter, FirstFooter, FirstHeader
  849. //
  850. // Formatting Code | Description
  851. // ------------------------+-------------------------------------------------------------------------
  852. // && | The character "&"
  853. // |
  854. // &font-size | Size of the text font, where font-size is a decimal font size in points
  855. // |
  856. // &"font name,font type" | A text font-name string, font name, and a text font-type string,
  857. // | font type
  858. // |
  859. // &"-,Regular" | Regular text format. Toggles bold and italic modes to off
  860. // |
  861. // &A | Current worksheet's tab name
  862. // |
  863. // &B or &"-,Bold" | Bold text format, from off to on, or vice versa. The default mode is off
  864. // |
  865. // &D | Current date
  866. // |
  867. // &C | Center section
  868. // |
  869. // &E | Double-underline text format
  870. // |
  871. // &F | Current workbook's file name
  872. // |
  873. // &G | Drawing object as background
  874. // |
  875. // &H | Shadow text format
  876. // |
  877. // &I or &"-,Italic" | Italic text format
  878. // |
  879. // &K | Text font color
  880. // |
  881. // | An RGB Color is specified as RRGGBB
  882. // |
  883. // | A Theme Color is specified as TTSNNN where TT is the theme color Id,
  884. // | S is either "+" or "-" of the tint/shade value, and NNN is the
  885. // | tint/shade value
  886. // |
  887. // &L | Left section
  888. // |
  889. // &N | Total number of pages
  890. // |
  891. // &O | Outline text format
  892. // |
  893. // &P[[+|-]n] | Without the optional suffix, the current page number in decimal
  894. // |
  895. // &R | Right section
  896. // |
  897. // &S | Strikethrough text format
  898. // |
  899. // &T | Current time
  900. // |
  901. // &U | Single-underline text format. If double-underline mode is on, the next
  902. // | occurrence in a section specifier toggles double-underline mode to off;
  903. // | otherwise, it toggles single-underline mode, from off to on, or vice
  904. // | versa. The default mode is off
  905. // |
  906. // &X | Superscript text format
  907. // |
  908. // &Y | Subscript text format
  909. // |
  910. // &Z | Current workbook's file path
  911. //
  912. // For example:
  913. //
  914. // err := f.SetHeaderFooter("Sheet1", &excelize.FormatHeaderFooter{
  915. // DifferentFirst: true,
  916. // DifferentOddEven: true,
  917. // OddHeader: "&R&P",
  918. // OddFooter: "&C&F",
  919. // EvenHeader: "&L&P",
  920. // EvenFooter: "&L&D&R&T",
  921. // FirstHeader: `&CCenter &"-,Bold"Bold&"-,Regular"HeaderU+000A&D`,
  922. // })
  923. //
  924. // This example shows:
  925. //
  926. // - The first page has its own header and footer
  927. //
  928. // - Odd and even-numbered pages have different headers and footers
  929. //
  930. // - Current page number in the right section of odd-page headers
  931. //
  932. // - Current workbook's file name in the center section of odd-page footers
  933. //
  934. // - Current page number in the left section of even-page headers
  935. //
  936. // - Current date in the left section and the current time in the right section
  937. // of even-page footers
  938. //
  939. // - The text "Center Bold Header" on the first line of the center section of
  940. // the first page, and the date on the second line of the center section of
  941. // that same page
  942. //
  943. // - No footer on the first page
  944. //
  945. func (f *File) SetHeaderFooter(sheet string, settings *FormatHeaderFooter) error {
  946. xlsx, err := f.workSheetReader(sheet)
  947. if err != nil {
  948. return err
  949. }
  950. if settings == nil {
  951. xlsx.HeaderFooter = nil
  952. return err
  953. }
  954. v := reflect.ValueOf(*settings)
  955. // Check 6 string type fields: OddHeader, OddFooter, EvenHeader, EvenFooter,
  956. // FirstFooter, FirstHeader
  957. for i := 4; i < v.NumField()-1; i++ {
  958. if v.Field(i).Len() >= 255 {
  959. return fmt.Errorf("field %s must be less than 255 characters", v.Type().Field(i).Name)
  960. }
  961. }
  962. xlsx.HeaderFooter = &xlsxHeaderFooter{
  963. AlignWithMargins: settings.AlignWithMargins,
  964. DifferentFirst: settings.DifferentFirst,
  965. DifferentOddEven: settings.DifferentOddEven,
  966. ScaleWithDoc: settings.ScaleWithDoc,
  967. OddHeader: settings.OddHeader,
  968. OddFooter: settings.OddFooter,
  969. EvenHeader: settings.EvenHeader,
  970. EvenFooter: settings.EvenFooter,
  971. FirstFooter: settings.FirstFooter,
  972. FirstHeader: settings.FirstHeader,
  973. }
  974. return err
  975. }
  976. // ProtectSheet provides a function to prevent other users from accidentally
  977. // or deliberately changing, moving, or deleting data in a worksheet. For
  978. // example, protect Sheet1 with protection settings:
  979. //
  980. // err := f.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{
  981. // Password: "password",
  982. // EditScenarios: false,
  983. // })
  984. //
  985. func (f *File) ProtectSheet(sheet string, settings *FormatSheetProtection) error {
  986. xlsx, err := f.workSheetReader(sheet)
  987. if err != nil {
  988. return err
  989. }
  990. if settings == nil {
  991. settings = &FormatSheetProtection{
  992. EditObjects: true,
  993. EditScenarios: true,
  994. SelectLockedCells: true,
  995. }
  996. }
  997. xlsx.SheetProtection = &xlsxSheetProtection{
  998. AutoFilter: settings.AutoFilter,
  999. DeleteColumns: settings.DeleteColumns,
  1000. DeleteRows: settings.DeleteRows,
  1001. FormatCells: settings.FormatCells,
  1002. FormatColumns: settings.FormatColumns,
  1003. FormatRows: settings.FormatRows,
  1004. InsertColumns: settings.InsertColumns,
  1005. InsertHyperlinks: settings.InsertHyperlinks,
  1006. InsertRows: settings.InsertRows,
  1007. Objects: settings.EditObjects,
  1008. PivotTables: settings.PivotTables,
  1009. Scenarios: settings.EditScenarios,
  1010. SelectLockedCells: settings.SelectLockedCells,
  1011. SelectUnlockedCells: settings.SelectUnlockedCells,
  1012. Sheet: true,
  1013. Sort: settings.Sort,
  1014. }
  1015. if settings.Password != "" {
  1016. xlsx.SheetProtection.Password = genSheetPasswd(settings.Password)
  1017. }
  1018. return err
  1019. }
  1020. // UnprotectSheet provides a function to unprotect an Excel worksheet.
  1021. func (f *File) UnprotectSheet(sheet string) error {
  1022. xlsx, err := f.workSheetReader(sheet)
  1023. if err != nil {
  1024. return err
  1025. }
  1026. xlsx.SheetProtection = nil
  1027. return err
  1028. }
  1029. // trimSheetName provides a function to trim invaild characters by given worksheet
  1030. // name.
  1031. func trimSheetName(name string) string {
  1032. if strings.ContainsAny(name, ":\\/?*[]") || utf8.RuneCountInString(name) > 31 {
  1033. r := make([]rune, 0, 31)
  1034. for _, v := range name {
  1035. switch v {
  1036. case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[]
  1037. continue
  1038. default:
  1039. r = append(r, v)
  1040. }
  1041. if len(r) == 31 {
  1042. break
  1043. }
  1044. }
  1045. name = string(r)
  1046. }
  1047. return name
  1048. }
  1049. // PageLayoutOption is an option of a page layout of a worksheet. See
  1050. // SetPageLayout().
  1051. type PageLayoutOption interface {
  1052. setPageLayout(layout *xlsxPageSetUp)
  1053. }
  1054. // PageLayoutOptionPtr is a writable PageLayoutOption. See GetPageLayout().
  1055. type PageLayoutOptionPtr interface {
  1056. PageLayoutOption
  1057. getPageLayout(layout *xlsxPageSetUp)
  1058. }
  1059. type (
  1060. // PageLayoutOrientation defines the orientation of page layout for a
  1061. // worksheet.
  1062. PageLayoutOrientation string
  1063. // PageLayoutPaperSize defines the paper size of the worksheet
  1064. PageLayoutPaperSize int
  1065. // FitToHeight specified number of vertical pages to fit on
  1066. FitToHeight int
  1067. // FitToWidth specified number of horizontal pages to fit on
  1068. FitToWidth int
  1069. )
  1070. const (
  1071. // OrientationPortrait indicates page layout orientation id portrait.
  1072. OrientationPortrait = "portrait"
  1073. // OrientationLandscape indicates page layout orientation id landscape.
  1074. OrientationLandscape = "landscape"
  1075. )
  1076. // setPageLayout provides a method to set the orientation for the worksheet.
  1077. func (o PageLayoutOrientation) setPageLayout(ps *xlsxPageSetUp) {
  1078. ps.Orientation = string(o)
  1079. }
  1080. // getPageLayout provides a method to get the orientation for the worksheet.
  1081. func (o *PageLayoutOrientation) getPageLayout(ps *xlsxPageSetUp) {
  1082. // Excel default: portrait
  1083. if ps == nil || ps.Orientation == "" {
  1084. *o = OrientationPortrait
  1085. return
  1086. }
  1087. *o = PageLayoutOrientation(ps.Orientation)
  1088. }
  1089. // setPageLayout provides a method to set the paper size for the worksheet.
  1090. func (p PageLayoutPaperSize) setPageLayout(ps *xlsxPageSetUp) {
  1091. ps.PaperSize = int(p)
  1092. }
  1093. // getPageLayout provides a method to get the paper size for the worksheet.
  1094. func (p *PageLayoutPaperSize) getPageLayout(ps *xlsxPageSetUp) {
  1095. // Excel default: 1
  1096. if ps == nil || ps.PaperSize == 0 {
  1097. *p = 1
  1098. return
  1099. }
  1100. *p = PageLayoutPaperSize(ps.PaperSize)
  1101. }
  1102. // setPageLayout provides a method to set the fit to height for the worksheet.
  1103. func (p FitToHeight) setPageLayout(ps *xlsxPageSetUp) {
  1104. if int(p) > 0 {
  1105. ps.FitToHeight = int(p)
  1106. }
  1107. }
  1108. // getPageLayout provides a method to get the fit to height for the worksheet.
  1109. func (p *FitToHeight) getPageLayout(ps *xlsxPageSetUp) {
  1110. if ps == nil || ps.FitToHeight == 0 {
  1111. *p = 1
  1112. return
  1113. }
  1114. *p = FitToHeight(ps.FitToHeight)
  1115. }
  1116. // setPageLayout provides a method to set the fit to width for the worksheet.
  1117. func (p FitToWidth) setPageLayout(ps *xlsxPageSetUp) {
  1118. if int(p) > 0 {
  1119. ps.FitToWidth = int(p)
  1120. }
  1121. }
  1122. // getPageLayout provides a method to get the fit to width for the worksheet.
  1123. func (p *FitToWidth) getPageLayout(ps *xlsxPageSetUp) {
  1124. if ps == nil || ps.FitToWidth == 0 {
  1125. *p = 1
  1126. return
  1127. }
  1128. *p = FitToWidth(ps.FitToWidth)
  1129. }
  1130. // SetPageLayout provides a function to sets worksheet page layout.
  1131. //
  1132. // Available options:
  1133. // PageLayoutOrientation(string)
  1134. // PageLayoutPaperSize(int)
  1135. //
  1136. // The following shows the paper size sorted by excelize index number:
  1137. //
  1138. // Index | Paper Size
  1139. // -------+-----------------------------------------------
  1140. // 1 | Letter paper (8.5 in. by 11 in.)
  1141. // 2 | Letter small paper (8.5 in. by 11 in.)
  1142. // 3 | Tabloid paper (11 in. by 17 in.)
  1143. // 4 | Ledger paper (17 in. by 11 in.)
  1144. // 5 | Legal paper (8.5 in. by 14 in.)
  1145. // 6 | Statement paper (5.5 in. by 8.5 in.)
  1146. // 7 | Executive paper (7.25 in. by 10.5 in.)
  1147. // 8 | A3 paper (297 mm by 420 mm)
  1148. // 9 | A4 paper (210 mm by 297 mm)
  1149. // 10 | A4 small paper (210 mm by 297 mm)
  1150. // 11 | A5 paper (148 mm by 210 mm)
  1151. // 12 | B4 paper (250 mm by 353 mm)
  1152. // 13 | B5 paper (176 mm by 250 mm)
  1153. // 14 | Folio paper (8.5 in. by 13 in.)
  1154. // 15 | Quarto paper (215 mm by 275 mm)
  1155. // 16 | Standard paper (10 in. by 14 in.)
  1156. // 17 | Standard paper (11 in. by 17 in.)
  1157. // 18 | Note paper (8.5 in. by 11 in.)
  1158. // 19 | #9 envelope (3.875 in. by 8.875 in.)
  1159. // 20 | #10 envelope (4.125 in. by 9.5 in.)
  1160. // 21 | #11 envelope (4.5 in. by 10.375 in.)
  1161. // 22 | #12 envelope (4.75 in. by 11 in.)
  1162. // 23 | #14 envelope (5 in. by 11.5 in.)
  1163. // 24 | C paper (17 in. by 22 in.)
  1164. // 25 | D paper (22 in. by 34 in.)
  1165. // 26 | E paper (34 in. by 44 in.)
  1166. // 27 | DL envelope (110 mm by 220 mm)
  1167. // 28 | C5 envelope (162 mm by 229 mm)
  1168. // 29 | C3 envelope (324 mm by 458 mm)
  1169. // 30 | C4 envelope (229 mm by 324 mm)
  1170. // 31 | C6 envelope (114 mm by 162 mm)
  1171. // 32 | C65 envelope (114 mm by 229 mm)
  1172. // 33 | B4 envelope (250 mm by 353 mm)
  1173. // 34 | B5 envelope (176 mm by 250 mm)
  1174. // 35 | B6 envelope (176 mm by 125 mm)
  1175. // 36 | Italy envelope (110 mm by 230 mm)
  1176. // 37 | Monarch envelope (3.875 in. by 7.5 in.).
  1177. // 38 | 6 3/4 envelope (3.625 in. by 6.5 in.)
  1178. // 39 | US standard fanfold (14.875 in. by 11 in.)
  1179. // 40 | German standard fanfold (8.5 in. by 12 in.)
  1180. // 41 | German legal fanfold (8.5 in. by 13 in.)
  1181. // 42 | ISO B4 (250 mm by 353 mm)
  1182. // 43 | Japanese postcard (100 mm by 148 mm)
  1183. // 44 | Standard paper (9 in. by 11 in.)
  1184. // 45 | Standard paper (10 in. by 11 in.)
  1185. // 46 | Standard paper (15 in. by 11 in.)
  1186. // 47 | Invite envelope (220 mm by 220 mm)
  1187. // 50 | Letter extra paper (9.275 in. by 12 in.)
  1188. // 51 | Legal extra paper (9.275 in. by 15 in.)
  1189. // 52 | Tabloid extra paper (11.69 in. by 18 in.)
  1190. // 53 | A4 extra paper (236 mm by 322 mm)
  1191. // 54 | Letter transverse paper (8.275 in. by 11 in.)
  1192. // 55 | A4 transverse paper (210 mm by 297 mm)
  1193. // 56 | Letter extra transverse paper (9.275 in. by 12 in.)
  1194. // 57 | SuperA/SuperA/A4 paper (227 mm by 356 mm)
  1195. // 58 | SuperB/SuperB/A3 paper (305 mm by 487 mm)
  1196. // 59 | Letter plus paper (8.5 in. by 12.69 in.)
  1197. // 60 | A4 plus paper (210 mm by 330 mm)
  1198. // 61 | A5 transverse paper (148 mm by 210 mm)
  1199. // 62 | JIS B5 transverse paper (182 mm by 257 mm)
  1200. // 63 | A3 extra paper (322 mm by 445 mm)
  1201. // 64 | A5 extra paper (174 mm by 235 mm)
  1202. // 65 | ISO B5 extra paper (201 mm by 276 mm)
  1203. // 66 | A2 paper (420 mm by 594 mm)
  1204. // 67 | A3 transverse paper (297 mm by 420 mm)
  1205. // 68 | A3 extra transverse paper (322 mm by 445 mm)
  1206. // 69 | Japanese Double Postcard (200 mm x 148 mm)
  1207. // 70 | A6 (105 mm x 148 mm)
  1208. // 71 | Japanese Envelope Kaku #2
  1209. // 72 | Japanese Envelope Kaku #3
  1210. // 73 | Japanese Envelope Chou #3
  1211. // 74 | Japanese Envelope Chou #4
  1212. // 75 | Letter Rotated (11in x 8 1/2 11 in)
  1213. // 76 | A3 Rotated (420 mm x 297 mm)
  1214. // 77 | A4 Rotated (297 mm x 210 mm)
  1215. // 78 | A5 Rotated (210 mm x 148 mm)
  1216. // 79 | B4 (JIS) Rotated (364 mm x 257 mm)
  1217. // 80 | B5 (JIS) Rotated (257 mm x 182 mm)
  1218. // 81 | Japanese Postcard Rotated (148 mm x 100 mm)
  1219. // 82 | Double Japanese Postcard Rotated (148 mm x 200 mm)
  1220. // 83 | A6 Rotated (148 mm x 105 mm)
  1221. // 84 | Japanese Envelope Kaku #2 Rotated
  1222. // 85 | Japanese Envelope Kaku #3 Rotated
  1223. // 86 | Japanese Envelope Chou #3 Rotated
  1224. // 87 | Japanese Envelope Chou #4 Rotated
  1225. // 88 | B6 (JIS) (128 mm x 182 mm)
  1226. // 89 | B6 (JIS) Rotated (182 mm x 128 mm)
  1227. // 90 | (12 in x 11 in)
  1228. // 91 | Japanese Envelope You #4
  1229. // 92 | Japanese Envelope You #4 Rotated
  1230. // 93 | PRC 16K (146 mm x 215 mm)
  1231. // 94 | PRC 32K (97 mm x 151 mm)
  1232. // 95 | PRC 32K(Big) (97 mm x 151 mm)
  1233. // 96 | PRC Envelope #1 (102 mm x 165 mm)
  1234. // 97 | PRC Envelope #2 (102 mm x 176 mm)
  1235. // 98 | PRC Envelope #3 (125 mm x 176 mm)
  1236. // 99 | PRC Envelope #4 (110 mm x 208 mm)
  1237. // 100 | PRC Envelope #5 (110 mm x 220 mm)
  1238. // 101 | PRC Envelope #6 (120 mm x 230 mm)
  1239. // 102 | PRC Envelope #7 (160 mm x 230 mm)
  1240. // 103 | PRC Envelope #8 (120 mm x 309 mm)
  1241. // 104 | PRC Envelope #9 (229 mm x 324 mm)
  1242. // 105 | PRC Envelope #10 (324 mm x 458 mm)
  1243. // 106 | PRC 16K Rotated
  1244. // 107 | PRC 32K Rotated
  1245. // 108 | PRC 32K(Big) Rotated
  1246. // 109 | PRC Envelope #1 Rotated (165 mm x 102 mm)
  1247. // 110 | PRC Envelope #2 Rotated (176 mm x 102 mm)
  1248. // 111 | PRC Envelope #3 Rotated (176 mm x 125 mm)
  1249. // 112 | PRC Envelope #4 Rotated (208 mm x 110 mm)
  1250. // 113 | PRC Envelope #5 Rotated (220 mm x 110 mm)
  1251. // 114 | PRC Envelope #6 Rotated (230 mm x 120 mm)
  1252. // 115 | PRC Envelope #7 Rotated (230 mm x 160 mm)
  1253. // 116 | PRC Envelope #8 Rotated (309 mm x 120 mm)
  1254. // 117 | PRC Envelope #9 Rotated (324 mm x 229 mm)
  1255. // 118 | PRC Envelope #10 Rotated (458 mm x 324 mm)
  1256. //
  1257. func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error {
  1258. s, err := f.workSheetReader(sheet)
  1259. if err != nil {
  1260. return err
  1261. }
  1262. ps := s.PageSetUp
  1263. if ps == nil {
  1264. ps = new(xlsxPageSetUp)
  1265. s.PageSetUp = ps
  1266. }
  1267. for _, opt := range opts {
  1268. opt.setPageLayout(ps)
  1269. }
  1270. return err
  1271. }
  1272. // GetPageLayout provides a function to gets worksheet page layout.
  1273. //
  1274. // Available options:
  1275. // PageLayoutOrientation(string)
  1276. // PageLayoutPaperSize(int)
  1277. // FitToHeight(int)
  1278. // FitToWidth(int)
  1279. func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error {
  1280. s, err := f.workSheetReader(sheet)
  1281. if err != nil {
  1282. return err
  1283. }
  1284. ps := s.PageSetUp
  1285. for _, opt := range opts {
  1286. opt.getPageLayout(ps)
  1287. }
  1288. return err
  1289. }
  1290. // SetDefinedName provides a function to set the defined names of the workbook
  1291. // or worksheet. If not specified scope, the default scope is workbook.
  1292. // For example:
  1293. //
  1294. // f.SetDefinedName(&excelize.DefinedName{
  1295. // Name: "Amount",
  1296. // RefersTo: "Sheet1!$A$2:$D$5",
  1297. // Comment: "defined name comment",
  1298. // Scope: "Sheet2",
  1299. // })
  1300. //
  1301. func (f *File) SetDefinedName(definedName *DefinedName) error {
  1302. wb := f.workbookReader()
  1303. d := xlsxDefinedName{
  1304. Name: definedName.Name,
  1305. Comment: definedName.Comment,
  1306. Data: definedName.RefersTo,
  1307. }
  1308. if definedName.Scope != "" {
  1309. if sheetID := f.getSheetID(definedName.Scope); sheetID != 0 {
  1310. sheetID--
  1311. d.LocalSheetID = &sheetID
  1312. }
  1313. }
  1314. if wb.DefinedNames != nil {
  1315. for _, dn := range wb.DefinedNames.DefinedName {
  1316. var scope string
  1317. if dn.LocalSheetID != nil {
  1318. scope = f.getSheetNameByID(*dn.LocalSheetID + 1)
  1319. }
  1320. if scope == definedName.Scope && dn.Name == definedName.Name {
  1321. return errors.New("the same name already exists on the scope")
  1322. }
  1323. }
  1324. wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName, d)
  1325. return nil
  1326. }
  1327. wb.DefinedNames = &xlsxDefinedNames{
  1328. DefinedName: []xlsxDefinedName{d},
  1329. }
  1330. return nil
  1331. }
  1332. // DeleteDefinedName provides a function to delete the defined names of the
  1333. // workbook or worksheet. If not specified scope, the default scope is
  1334. // workbook. For example:
  1335. //
  1336. // f.DeleteDefinedName(&excelize.DefinedName{
  1337. // Name: "Amount",
  1338. // Scope: "Sheet2",
  1339. // })
  1340. //
  1341. func (f *File) DeleteDefinedName(definedName *DefinedName) error {
  1342. wb := f.workbookReader()
  1343. if wb.DefinedNames != nil {
  1344. for idx, dn := range wb.DefinedNames.DefinedName {
  1345. var scope string
  1346. if dn.LocalSheetID != nil {
  1347. scope = f.getSheetNameByID(*dn.LocalSheetID + 1)
  1348. }
  1349. if scope == definedName.Scope && dn.Name == definedName.Name {
  1350. wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName[:idx], wb.DefinedNames.DefinedName[idx+1:]...)
  1351. return nil
  1352. }
  1353. }
  1354. }
  1355. return errors.New("no defined name on the scope")
  1356. }
  1357. // GetDefinedName provides a function to get the defined names of the workbook
  1358. // or worksheet.
  1359. func (f *File) GetDefinedName() []DefinedName {
  1360. var definedNames []DefinedName
  1361. wb := f.workbookReader()
  1362. if wb.DefinedNames != nil {
  1363. for _, dn := range wb.DefinedNames.DefinedName {
  1364. definedName := DefinedName{
  1365. Name: dn.Name,
  1366. Comment: dn.Comment,
  1367. RefersTo: dn.Data,
  1368. Scope: "Workbook",
  1369. }
  1370. if dn.LocalSheetID != nil && *dn.LocalSheetID >= 0 {
  1371. definedName.Scope = f.getSheetNameByID(*dn.LocalSheetID + 1)
  1372. }
  1373. definedNames = append(definedNames, definedName)
  1374. }
  1375. }
  1376. return definedNames
  1377. }
  1378. // GroupSheets provides a function to group worksheets by given worksheets
  1379. // name. Group worksheets must contain an active worksheet.
  1380. func (f *File) GroupSheets(sheets []string) error {
  1381. // check an active worksheet in group worksheets
  1382. var inActiveSheet bool
  1383. activeSheet := f.GetActiveSheetIndex()
  1384. sheetMap := f.GetSheetList()
  1385. for idx, sheetName := range sheetMap {
  1386. for _, s := range sheets {
  1387. if s == sheetName && idx == activeSheet {
  1388. inActiveSheet = true
  1389. }
  1390. }
  1391. }
  1392. if !inActiveSheet {
  1393. return errors.New("group worksheet must contain an active worksheet")
  1394. }
  1395. // check worksheet exists
  1396. ws := []*xlsxWorksheet{}
  1397. for _, sheet := range sheets {
  1398. xlsx, err := f.workSheetReader(sheet)
  1399. if err != nil {
  1400. return err
  1401. }
  1402. ws = append(ws, xlsx)
  1403. }
  1404. for _, s := range ws {
  1405. sheetViews := s.SheetViews.SheetView
  1406. if len(sheetViews) > 0 {
  1407. for idx := range sheetViews {
  1408. s.SheetViews.SheetView[idx].TabSelected = true
  1409. }
  1410. continue
  1411. }
  1412. }
  1413. return nil
  1414. }
  1415. // UngroupSheets provides a function to ungroup worksheets.
  1416. func (f *File) UngroupSheets() error {
  1417. activeSheet := f.GetActiveSheetIndex()
  1418. for index, sheet := range f.GetSheetList() {
  1419. if activeSheet == index {
  1420. continue
  1421. }
  1422. ws, _ := f.workSheetReader(sheet)
  1423. sheetViews := ws.SheetViews.SheetView
  1424. if len(sheetViews) > 0 {
  1425. for idx := range sheetViews {
  1426. ws.SheetViews.SheetView[idx].TabSelected = false
  1427. }
  1428. }
  1429. }
  1430. return nil
  1431. }
  1432. // InsertPageBreak create a page break to determine where the printed page
  1433. // ends and where begins the next one by given worksheet name and axis, so the
  1434. // content before the page break will be printed on one page and after the
  1435. // page break on another.
  1436. func (f *File) InsertPageBreak(sheet, cell string) (err error) {
  1437. var ws *xlsxWorksheet
  1438. var row, col int
  1439. var rowBrk, colBrk = -1, -1
  1440. if ws, err = f.workSheetReader(sheet); err != nil {
  1441. return
  1442. }
  1443. if col, row, err = CellNameToCoordinates(cell); err != nil {
  1444. return
  1445. }
  1446. col--
  1447. row--
  1448. if col == row && col == 0 {
  1449. return
  1450. }
  1451. if ws.RowBreaks == nil {
  1452. ws.RowBreaks = &xlsxBreaks{}
  1453. }
  1454. if ws.ColBreaks == nil {
  1455. ws.ColBreaks = &xlsxBreaks{}
  1456. }
  1457. for idx, brk := range ws.RowBreaks.Brk {
  1458. if brk.ID == row {
  1459. rowBrk = idx
  1460. }
  1461. }
  1462. for idx, brk := range ws.ColBreaks.Brk {
  1463. if brk.ID == col {
  1464. colBrk = idx
  1465. }
  1466. }
  1467. if row != 0 && rowBrk == -1 {
  1468. ws.RowBreaks.Brk = append(ws.RowBreaks.Brk, &xlsxBrk{
  1469. ID: row,
  1470. Max: 16383,
  1471. Man: true,
  1472. })
  1473. ws.RowBreaks.ManualBreakCount++
  1474. }
  1475. if col != 0 && colBrk == -1 {
  1476. ws.ColBreaks.Brk = append(ws.ColBreaks.Brk, &xlsxBrk{
  1477. ID: col,
  1478. Max: 1048575,
  1479. Man: true,
  1480. })
  1481. ws.ColBreaks.ManualBreakCount++
  1482. }
  1483. ws.RowBreaks.Count = len(ws.RowBreaks.Brk)
  1484. ws.ColBreaks.Count = len(ws.ColBreaks.Brk)
  1485. return
  1486. }
  1487. // RemovePageBreak remove a page break by given worksheet name and axis.
  1488. func (f *File) RemovePageBreak(sheet, cell string) (err error) {
  1489. var ws *xlsxWorksheet
  1490. var row, col int
  1491. if ws, err = f.workSheetReader(sheet); err != nil {
  1492. return
  1493. }
  1494. if col, row, err = CellNameToCoordinates(cell); err != nil {
  1495. return
  1496. }
  1497. col--
  1498. row--
  1499. if col == row && col == 0 {
  1500. return
  1501. }
  1502. removeBrk := func(ID int, brks []*xlsxBrk) []*xlsxBrk {
  1503. for i, brk := range brks {
  1504. if brk.ID == ID {
  1505. brks = append(brks[:i], brks[i+1:]...)
  1506. }
  1507. }
  1508. return brks
  1509. }
  1510. if ws.RowBreaks == nil || ws.ColBreaks == nil {
  1511. return
  1512. }
  1513. rowBrks := len(ws.RowBreaks.Brk)
  1514. colBrks := len(ws.ColBreaks.Brk)
  1515. if rowBrks > 0 && rowBrks == colBrks {
  1516. ws.RowBreaks.Brk = removeBrk(row, ws.RowBreaks.Brk)
  1517. ws.ColBreaks.Brk = removeBrk(col, ws.ColBreaks.Brk)
  1518. ws.RowBreaks.Count = len(ws.RowBreaks.Brk)
  1519. ws.ColBreaks.Count = len(ws.ColBreaks.Brk)
  1520. ws.RowBreaks.ManualBreakCount--
  1521. ws.ColBreaks.ManualBreakCount--
  1522. return
  1523. }
  1524. if rowBrks > 0 && rowBrks > colBrks {
  1525. ws.RowBreaks.Brk = removeBrk(row, ws.RowBreaks.Brk)
  1526. ws.RowBreaks.Count = len(ws.RowBreaks.Brk)
  1527. ws.RowBreaks.ManualBreakCount--
  1528. return
  1529. }
  1530. if colBrks > 0 && colBrks > rowBrks {
  1531. ws.ColBreaks.Brk = removeBrk(col, ws.ColBreaks.Brk)
  1532. ws.ColBreaks.Count = len(ws.ColBreaks.Brk)
  1533. ws.ColBreaks.ManualBreakCount--
  1534. }
  1535. return
  1536. }
  1537. // relsReader provides a function to get the pointer to the structure
  1538. // after deserialization of xl/worksheets/_rels/sheet%d.xml.rels.
  1539. func (f *File) relsReader(path string) *xlsxRelationships {
  1540. var err error
  1541. if f.Relationships[path] == nil {
  1542. _, ok := f.XLSX[path]
  1543. if ok {
  1544. c := xlsxRelationships{}
  1545. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
  1546. Decode(&c); err != nil && err != io.EOF {
  1547. log.Printf("xml decode error: %s", err)
  1548. }
  1549. f.Relationships[path] = &c
  1550. }
  1551. }
  1552. return f.Relationships[path]
  1553. }
  1554. // fillSheetData ensures there are enough rows, and columns in the chosen
  1555. // row to accept data. Missing rows are backfilled and given their row number
  1556. // Uses the last populated row as a hint for the size of the next row to add
  1557. func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) {
  1558. rowCount := len(xlsx.SheetData.Row)
  1559. sizeHint := 0
  1560. var ht float64
  1561. var customHeight bool
  1562. if xlsx.SheetFormatPr != nil {
  1563. ht = xlsx.SheetFormatPr.DefaultRowHeight
  1564. customHeight = true
  1565. }
  1566. if rowCount > 0 {
  1567. sizeHint = len(xlsx.SheetData.Row[rowCount-1].C)
  1568. }
  1569. if rowCount < row {
  1570. // append missing rows
  1571. for rowIdx := rowCount; rowIdx < row; rowIdx++ {
  1572. xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1, CustomHeight: customHeight, Ht: ht, C: make([]xlsxC, 0, sizeHint)})
  1573. }
  1574. }
  1575. rowData := &xlsx.SheetData.Row[row-1]
  1576. fillColumns(rowData, col, row)
  1577. }
  1578. func fillColumns(rowData *xlsxRow, col, row int) {
  1579. cellCount := len(rowData.C)
  1580. if cellCount < col {
  1581. for colIdx := cellCount; colIdx < col; colIdx++ {
  1582. cellName, _ := CoordinatesToCellName(colIdx+1, row)
  1583. rowData.C = append(rowData.C, xlsxC{R: cellName})
  1584. }
  1585. }
  1586. }
  1587. func makeContiguousColumns(xlsx *xlsxWorksheet, fromRow, toRow, colCount int) {
  1588. for ; fromRow < toRow; fromRow++ {
  1589. rowData := &xlsx.SheetData.Row[fromRow-1]
  1590. fillColumns(rowData, colCount, fromRow)
  1591. }
  1592. }