cell.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import (
  11. "encoding/xml"
  12. "fmt"
  13. "reflect"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. const (
  19. // STCellFormulaTypeArray defined the formula is an array formula.
  20. STCellFormulaTypeArray = "array"
  21. // STCellFormulaTypeDataTable defined the formula is a data table formula.
  22. STCellFormulaTypeDataTable = "dataTable"
  23. // STCellFormulaTypeNormal defined the formula is a regular cell formula.
  24. STCellFormulaTypeNormal = "normal"
  25. // STCellFormulaTypeShared defined the formula is part of a shared formula.
  26. STCellFormulaTypeShared = "shared"
  27. )
  28. // mergeCellsParser provides a function to check merged cells in worksheet by
  29. // given axis.
  30. func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
  31. axis = strings.ToUpper(axis)
  32. if xlsx.MergeCells != nil {
  33. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  34. if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
  35. axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
  36. }
  37. }
  38. }
  39. return axis
  40. }
  41. // SetCellValue provides a function to set value of a cell. The following
  42. // shows the supported data types:
  43. //
  44. // int
  45. // int8
  46. // int16
  47. // int32
  48. // int64
  49. // uint
  50. // uint8
  51. // uint16
  52. // uint32
  53. // uint64
  54. // float32
  55. // float64
  56. // string
  57. // []byte
  58. // time.Duration
  59. // time.Time
  60. // bool
  61. // nil
  62. //
  63. // Note that default date format is m/d/yy h:mm of time.Time type value. You can
  64. // set numbers format by SetCellStyle() method.
  65. func (f *File) SetCellValue(sheet, axis string, value interface{}) {
  66. switch t := value.(type) {
  67. case float32:
  68. f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float32)), 'f', -1, 32))
  69. case float64:
  70. f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float64)), 'f', -1, 64))
  71. case string:
  72. f.SetCellStr(sheet, axis, t)
  73. case []byte:
  74. f.SetCellStr(sheet, axis, string(t))
  75. case time.Duration:
  76. f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(time.Duration).Seconds()/86400), 'f', -1, 32))
  77. f.setDefaultTimeStyle(sheet, axis, 21)
  78. case time.Time:
  79. f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(timeToExcelTime(timeToUTCTime(value.(time.Time)))), 'f', -1, 64))
  80. f.setDefaultTimeStyle(sheet, axis, 22)
  81. case nil:
  82. f.SetCellStr(sheet, axis, "")
  83. case bool:
  84. f.SetCellBool(sheet, axis, bool(value.(bool)))
  85. default:
  86. f.setCellIntValue(sheet, axis, value)
  87. }
  88. }
  89. // setCellIntValue provides a function to set int value of a cell.
  90. func (f *File) setCellIntValue(sheet, axis string, value interface{}) {
  91. switch value.(type) {
  92. case int:
  93. f.SetCellInt(sheet, axis, value.(int))
  94. case int8:
  95. f.SetCellInt(sheet, axis, int(value.(int8)))
  96. case int16:
  97. f.SetCellInt(sheet, axis, int(value.(int16)))
  98. case int32:
  99. f.SetCellInt(sheet, axis, int(value.(int32)))
  100. case int64:
  101. f.SetCellInt(sheet, axis, int(value.(int64)))
  102. case uint:
  103. f.SetCellInt(sheet, axis, int(value.(uint)))
  104. case uint8:
  105. f.SetCellInt(sheet, axis, int(value.(uint8)))
  106. case uint16:
  107. f.SetCellInt(sheet, axis, int(value.(uint16)))
  108. case uint32:
  109. f.SetCellInt(sheet, axis, int(value.(uint32)))
  110. case uint64:
  111. f.SetCellInt(sheet, axis, int(value.(uint64)))
  112. default:
  113. f.SetCellStr(sheet, axis, fmt.Sprintf("%v", value))
  114. }
  115. }
  116. // SetCellBool provides a function to set bool type value of a cell by given
  117. // worksheet name, cell coordinates and cell value.
  118. func (f *File) SetCellBool(sheet, axis string, value bool) {
  119. xlsx := f.workSheetReader(sheet)
  120. axis = f.mergeCellsParser(xlsx, axis)
  121. col := string(strings.Map(letterOnlyMapF, axis))
  122. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  123. if err != nil {
  124. return
  125. }
  126. xAxis := row - 1
  127. yAxis := TitleToNumber(col)
  128. rows := xAxis + 1
  129. cell := yAxis + 1
  130. completeRow(xlsx, rows, cell)
  131. completeCol(xlsx, rows, cell)
  132. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  133. xlsx.SheetData.Row[xAxis].C[yAxis].T = "b"
  134. if value {
  135. xlsx.SheetData.Row[xAxis].C[yAxis].V = "1"
  136. } else {
  137. xlsx.SheetData.Row[xAxis].C[yAxis].V = "0"
  138. }
  139. }
  140. // GetCellValue provides a function to get formatted value from cell by given
  141. // worksheet name and axis in XLSX file. If it is possible to apply a format
  142. // to the cell value, it will do so, if not then an error will be returned,
  143. // along with the raw value of the cell.
  144. func (f *File) GetCellValue(sheet, axis string) string {
  145. xlsx := f.workSheetReader(sheet)
  146. axis = f.mergeCellsParser(xlsx, axis)
  147. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  148. if err != nil {
  149. return ""
  150. }
  151. xAxis := row - 1
  152. rows := len(xlsx.SheetData.Row)
  153. if rows > 1 {
  154. lastRow := xlsx.SheetData.Row[rows-1].R
  155. if lastRow >= rows {
  156. rows = lastRow
  157. }
  158. }
  159. if rows < xAxis {
  160. return ""
  161. }
  162. for k := range xlsx.SheetData.Row {
  163. if xlsx.SheetData.Row[k].R == row {
  164. for i := range xlsx.SheetData.Row[k].C {
  165. if axis == xlsx.SheetData.Row[k].C[i].R {
  166. val, _ := xlsx.SheetData.Row[k].C[i].getValueFrom(f, f.sharedStringsReader())
  167. return val
  168. }
  169. }
  170. }
  171. }
  172. return ""
  173. }
  174. // formattedValue provides a function to returns a value after formatted. If
  175. // it is possible to apply a format to the cell value, it will do so, if not
  176. // then an error will be returned, along with the raw value of the cell.
  177. func (f *File) formattedValue(s int, v string) string {
  178. if s == 0 {
  179. return v
  180. }
  181. styleSheet := f.stylesReader()
  182. ok := builtInNumFmtFunc[styleSheet.CellXfs.Xf[s].NumFmtID]
  183. if ok != nil {
  184. return ok(styleSheet.CellXfs.Xf[s].NumFmtID, v)
  185. }
  186. return v
  187. }
  188. // GetCellStyle provides a function to get cell style index by given worksheet
  189. // name and cell coordinates.
  190. func (f *File) GetCellStyle(sheet, axis string) int {
  191. xlsx := f.workSheetReader(sheet)
  192. axis = f.mergeCellsParser(xlsx, axis)
  193. col := string(strings.Map(letterOnlyMapF, axis))
  194. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  195. if err != nil {
  196. return 0
  197. }
  198. xAxis := row - 1
  199. yAxis := TitleToNumber(col)
  200. rows := xAxis + 1
  201. cell := yAxis + 1
  202. completeRow(xlsx, rows, cell)
  203. completeCol(xlsx, rows, cell)
  204. return f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  205. }
  206. // GetCellFormula provides a function to get formula from cell by given
  207. // worksheet name and axis in XLSX file.
  208. func (f *File) GetCellFormula(sheet, axis string) string {
  209. xlsx := f.workSheetReader(sheet)
  210. axis = f.mergeCellsParser(xlsx, axis)
  211. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  212. if err != nil {
  213. return ""
  214. }
  215. xAxis := row - 1
  216. rows := len(xlsx.SheetData.Row)
  217. if rows > 1 {
  218. lastRow := xlsx.SheetData.Row[rows-1].R
  219. if lastRow >= rows {
  220. rows = lastRow
  221. }
  222. }
  223. if rows < xAxis {
  224. return ""
  225. }
  226. for k := range xlsx.SheetData.Row {
  227. if xlsx.SheetData.Row[k].R == row {
  228. for i := range xlsx.SheetData.Row[k].C {
  229. if axis == xlsx.SheetData.Row[k].C[i].R {
  230. if xlsx.SheetData.Row[k].C[i].F.T == STCellFormulaTypeShared {
  231. return getSharedForumula(xlsx, xlsx.SheetData.Row[k].C[i].F.Si)
  232. }
  233. if xlsx.SheetData.Row[k].C[i].F != nil {
  234. return xlsx.SheetData.Row[k].C[i].F.Content
  235. }
  236. }
  237. }
  238. }
  239. }
  240. return ""
  241. }
  242. // getSharedForumula find a cell contains the same formula as another cell,
  243. // the "shared" value can be used for the t attribute and the si attribute can
  244. // be used to refer to the cell containing the formula. Two formulas are
  245. // considered to be the same when their respective representations in
  246. // R1C1-reference notation, are the same.
  247. //
  248. // Note that this function not validate ref tag to check the cell if or not in
  249. // allow area, and always return origin shared formula.
  250. func getSharedForumula(xlsx *xlsxWorksheet, si string) string {
  251. for k := range xlsx.SheetData.Row {
  252. for i := range xlsx.SheetData.Row[k].C {
  253. if xlsx.SheetData.Row[k].C[i].F == nil {
  254. continue
  255. }
  256. if xlsx.SheetData.Row[k].C[i].F.T != STCellFormulaTypeShared {
  257. continue
  258. }
  259. if xlsx.SheetData.Row[k].C[i].F.Si != si {
  260. continue
  261. }
  262. if xlsx.SheetData.Row[k].C[i].F.Ref != "" {
  263. return xlsx.SheetData.Row[k].C[i].F.Content
  264. }
  265. }
  266. }
  267. return ""
  268. }
  269. // SetCellFormula provides a function to set cell formula by given string and
  270. // worksheet name.
  271. func (f *File) SetCellFormula(sheet, axis, formula string) {
  272. xlsx := f.workSheetReader(sheet)
  273. axis = f.mergeCellsParser(xlsx, axis)
  274. col := string(strings.Map(letterOnlyMapF, axis))
  275. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  276. if err != nil {
  277. return
  278. }
  279. xAxis := row - 1
  280. yAxis := TitleToNumber(col)
  281. rows := xAxis + 1
  282. cell := yAxis + 1
  283. completeRow(xlsx, rows, cell)
  284. completeCol(xlsx, rows, cell)
  285. if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
  286. xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
  287. } else {
  288. f := xlsxF{
  289. Content: formula,
  290. }
  291. xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
  292. }
  293. }
  294. // SetCellHyperLink provides a function to set cell hyperlink by given
  295. // worksheet name and link URL address. LinkType defines two types of
  296. // hyperlink "External" for web site or "Location" for moving to one of cell
  297. // in this workbook. The below is example for external link.
  298. //
  299. // xlsx.SetCellHyperLink("Sheet1", "A3", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  300. // // Set underline and font color style for the cell.
  301. // style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
  302. // xlsx.SetCellStyle("Sheet1", "A3", "A3", style)
  303. //
  304. // A this is another example for "Location":
  305. //
  306. // xlsx.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")
  307. //
  308. func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) {
  309. xlsx := f.workSheetReader(sheet)
  310. axis = f.mergeCellsParser(xlsx, axis)
  311. linkTypes := map[string]xlsxHyperlink{
  312. "External": {},
  313. "Location": {Location: link},
  314. }
  315. hyperlink, ok := linkTypes[linkType]
  316. if !ok || axis == "" {
  317. return
  318. }
  319. hyperlink.Ref = axis
  320. if linkType == "External" {
  321. rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, linkType)
  322. hyperlink.RID = "rId" + strconv.Itoa(rID)
  323. }
  324. if xlsx.Hyperlinks == nil {
  325. xlsx.Hyperlinks = &xlsxHyperlinks{}
  326. }
  327. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
  328. }
  329. // GetCellHyperLink provides a function to get cell hyperlink by given
  330. // worksheet name and axis. Boolean type value link will be ture if the cell
  331. // has a hyperlink and the target is the address of the hyperlink. Otherwise,
  332. // the value of link will be false and the value of the target will be a blank
  333. // string. For example get hyperlink of Sheet1!H6:
  334. //
  335. // link, target := xlsx.GetCellHyperLink("Sheet1", "H6")
  336. //
  337. func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) {
  338. var link bool
  339. var target string
  340. xlsx := f.workSheetReader(sheet)
  341. axis = f.mergeCellsParser(xlsx, axis)
  342. if xlsx.Hyperlinks == nil || axis == "" {
  343. return link, target
  344. }
  345. for h := range xlsx.Hyperlinks.Hyperlink {
  346. if xlsx.Hyperlinks.Hyperlink[h].Ref == axis {
  347. link = true
  348. target = xlsx.Hyperlinks.Hyperlink[h].Location
  349. if xlsx.Hyperlinks.Hyperlink[h].RID != "" {
  350. target = f.getSheetRelationshipsTargetByID(sheet, xlsx.Hyperlinks.Hyperlink[h].RID)
  351. }
  352. }
  353. }
  354. return link, target
  355. }
  356. // MergeCell provides a function to merge cells by given coordinate area and
  357. // sheet name. For example create a merged cell of D3:E9 on Sheet1:
  358. //
  359. // xlsx.MergeCell("Sheet1", "D3", "E9")
  360. //
  361. // If you create a merged cell that overlaps with another existing merged cell,
  362. // those merged cells that already exist will be removed.
  363. func (f *File) MergeCell(sheet, hcell, vcell string) {
  364. if hcell == vcell {
  365. return
  366. }
  367. hcell = strings.ToUpper(hcell)
  368. vcell = strings.ToUpper(vcell)
  369. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  370. hcol := string(strings.Map(letterOnlyMapF, hcell))
  371. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  372. hyAxis := hrow - 1
  373. hxAxis := TitleToNumber(hcol)
  374. vcol := string(strings.Map(letterOnlyMapF, vcell))
  375. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  376. vyAxis := vrow - 1
  377. vxAxis := TitleToNumber(vcol)
  378. if vxAxis < hxAxis {
  379. hcell, vcell = vcell, hcell
  380. vxAxis, hxAxis = hxAxis, vxAxis
  381. }
  382. if vyAxis < hyAxis {
  383. hcell, vcell = vcell, hcell
  384. vyAxis, hyAxis = hyAxis, vyAxis
  385. }
  386. xlsx := f.workSheetReader(sheet)
  387. if xlsx.MergeCells != nil {
  388. mergeCell := xlsxMergeCell{}
  389. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  390. mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
  391. // Delete the merged cells of the overlapping area.
  392. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  393. if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) {
  394. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  395. } else if checkCellInArea(vcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[1], mergeCell.Ref) {
  396. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  397. }
  398. }
  399. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells, &mergeCell)
  400. } else {
  401. mergeCell := xlsxMergeCell{}
  402. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  403. mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
  404. mergeCells := xlsxMergeCells{}
  405. mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
  406. xlsx.MergeCells = &mergeCells
  407. }
  408. }
  409. // SetCellInt provides a function to set int type value of a cell by given
  410. // worksheet name, cell coordinates and cell value.
  411. func (f *File) SetCellInt(sheet, axis string, value int) {
  412. xlsx := f.workSheetReader(sheet)
  413. axis = f.mergeCellsParser(xlsx, axis)
  414. col := string(strings.Map(letterOnlyMapF, axis))
  415. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  416. if err != nil {
  417. return
  418. }
  419. xAxis := row - 1
  420. yAxis := TitleToNumber(col)
  421. rows := xAxis + 1
  422. cell := yAxis + 1
  423. completeRow(xlsx, rows, cell)
  424. completeCol(xlsx, rows, cell)
  425. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  426. xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
  427. xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
  428. }
  429. // prepareCellStyle provides a function to prepare style index of cell in
  430. // worksheet by given column index and style index.
  431. func (f *File) prepareCellStyle(xlsx *xlsxWorksheet, col, style int) int {
  432. if xlsx.Cols != nil && style == 0 {
  433. for _, v := range xlsx.Cols.Col {
  434. if v.Min <= col && col <= v.Max {
  435. style = v.Style
  436. }
  437. }
  438. }
  439. return style
  440. }
  441. // SetCellStr provides a function to set string type value of a cell. Total
  442. // number of characters that a cell can contain 32767 characters.
  443. func (f *File) SetCellStr(sheet, axis, value string) {
  444. xlsx := f.workSheetReader(sheet)
  445. axis = f.mergeCellsParser(xlsx, axis)
  446. if len(value) > 32767 {
  447. value = value[0:32767]
  448. }
  449. col := string(strings.Map(letterOnlyMapF, axis))
  450. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  451. if err != nil {
  452. return
  453. }
  454. xAxis := row - 1
  455. yAxis := TitleToNumber(col)
  456. rows := xAxis + 1
  457. cell := yAxis + 1
  458. completeRow(xlsx, rows, cell)
  459. completeCol(xlsx, rows, cell)
  460. // Leading space(s) character detection.
  461. if len(value) > 0 {
  462. if value[0] == 32 {
  463. xlsx.SheetData.Row[xAxis].C[yAxis].XMLSpace = xml.Attr{
  464. Name: xml.Name{Space: NameSpaceXML, Local: "space"},
  465. Value: "preserve",
  466. }
  467. }
  468. }
  469. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  470. xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
  471. xlsx.SheetData.Row[xAxis].C[yAxis].V = value
  472. }
  473. // SetCellDefault provides a function to set string type value of a cell as
  474. // default format without escaping the cell.
  475. func (f *File) SetCellDefault(sheet, axis, value string) {
  476. xlsx := f.workSheetReader(sheet)
  477. axis = f.mergeCellsParser(xlsx, axis)
  478. col := string(strings.Map(letterOnlyMapF, axis))
  479. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  480. if err != nil {
  481. return
  482. }
  483. xAxis := row - 1
  484. yAxis := TitleToNumber(col)
  485. rows := xAxis + 1
  486. cell := yAxis + 1
  487. completeRow(xlsx, rows, cell)
  488. completeCol(xlsx, rows, cell)
  489. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  490. xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
  491. xlsx.SheetData.Row[xAxis].C[yAxis].V = value
  492. }
  493. // SetSheetRow writes an array to row by given worksheet name, starting
  494. // coordinate and a pointer to array type 'slice'. For example, writes an
  495. // array to row 6 start with the cell B6 on Sheet1:
  496. //
  497. // xlsx.SetSheetRow("Sheet1", "B6", &[]interface{}{"1", nil, 2})
  498. //
  499. func (f *File) SetSheetRow(sheet, axis string, slice interface{}) {
  500. xlsx := f.workSheetReader(sheet)
  501. axis = f.mergeCellsParser(xlsx, axis)
  502. col := string(strings.Map(letterOnlyMapF, axis))
  503. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  504. if err != nil {
  505. return
  506. }
  507. // Make sure 'slice' is a Ptr to Slice
  508. v := reflect.ValueOf(slice)
  509. if v.Kind() != reflect.Ptr {
  510. return
  511. }
  512. v = v.Elem()
  513. if v.Kind() != reflect.Slice {
  514. return
  515. }
  516. xAxis := row - 1
  517. yAxis := TitleToNumber(col)
  518. rows := xAxis + 1
  519. cell := yAxis + 1
  520. completeRow(xlsx, rows, cell)
  521. completeCol(xlsx, rows, cell)
  522. idx := 0
  523. for i := cell - 1; i < v.Len()+cell-1; i++ {
  524. c := ToAlphaString(i) + strconv.Itoa(row)
  525. f.SetCellValue(sheet, c, v.Index(idx).Interface())
  526. idx++
  527. }
  528. }
  529. // checkCellInArea provides a function to determine if a given coordinate is
  530. // within an area.
  531. func checkCellInArea(cell, area string) bool {
  532. cell = strings.ToUpper(cell)
  533. area = strings.ToUpper(area)
  534. ref := strings.Split(area, ":")
  535. if len(ref) < 2 {
  536. return false
  537. }
  538. from := ref[0]
  539. to := ref[1]
  540. col, row := getCellColRow(cell)
  541. fromCol, fromRow := getCellColRow(from)
  542. toCol, toRow := getCellColRow(to)
  543. return axisLowerOrEqualThan(fromCol, col) && axisLowerOrEqualThan(col, toCol) && axisLowerOrEqualThan(fromRow, row) && axisLowerOrEqualThan(row, toRow)
  544. }