sheet.go 54 KB

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