cell.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import (
  11. "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 == nil {
  231. continue
  232. }
  233. if xlsx.SheetData.Row[k].C[i].F.T == STCellFormulaTypeShared {
  234. return getSharedForumula(xlsx, xlsx.SheetData.Row[k].C[i].F.Si)
  235. }
  236. return xlsx.SheetData.Row[k].C[i].F.Content
  237. }
  238. }
  239. }
  240. }
  241. return ""
  242. }
  243. // getSharedForumula find a cell contains the same formula as another cell,
  244. // the "shared" value can be used for the t attribute and the si attribute can
  245. // be used to refer to the cell containing the formula. Two formulas are
  246. // considered to be the same when their respective representations in
  247. // R1C1-reference notation, are the same.
  248. //
  249. // Note that this function not validate ref tag to check the cell if or not in
  250. // allow area, and always return origin shared formula.
  251. func getSharedForumula(xlsx *xlsxWorksheet, si string) string {
  252. for k := range xlsx.SheetData.Row {
  253. for i := range xlsx.SheetData.Row[k].C {
  254. if xlsx.SheetData.Row[k].C[i].F == nil {
  255. continue
  256. }
  257. if xlsx.SheetData.Row[k].C[i].F.T != STCellFormulaTypeShared {
  258. continue
  259. }
  260. if xlsx.SheetData.Row[k].C[i].F.Si != si {
  261. continue
  262. }
  263. if xlsx.SheetData.Row[k].C[i].F.Ref != "" {
  264. return xlsx.SheetData.Row[k].C[i].F.Content
  265. }
  266. }
  267. }
  268. return ""
  269. }
  270. // SetCellFormula provides a function to set cell formula by given string and
  271. // worksheet name.
  272. func (f *File) SetCellFormula(sheet, axis, formula string) {
  273. xlsx := f.workSheetReader(sheet)
  274. axis = f.mergeCellsParser(xlsx, axis)
  275. col := string(strings.Map(letterOnlyMapF, axis))
  276. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  277. if err != nil {
  278. return
  279. }
  280. xAxis := row - 1
  281. yAxis := TitleToNumber(col)
  282. rows := xAxis + 1
  283. cell := yAxis + 1
  284. completeRow(xlsx, rows, cell)
  285. completeCol(xlsx, rows, cell)
  286. if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
  287. xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
  288. } else {
  289. f := xlsxF{
  290. Content: formula,
  291. }
  292. xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
  293. }
  294. }
  295. // SetCellHyperLink provides a function to set cell hyperlink by given
  296. // worksheet name and link URL address. LinkType defines two types of
  297. // hyperlink "External" for web site or "Location" for moving to one of cell
  298. // in this workbook. The below is example for external link.
  299. //
  300. // xlsx.SetCellHyperLink("Sheet1", "A3", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  301. // // Set underline and font color style for the cell.
  302. // style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
  303. // xlsx.SetCellStyle("Sheet1", "A3", "A3", style)
  304. //
  305. // A this is another example for "Location":
  306. //
  307. // xlsx.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")
  308. //
  309. func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) {
  310. xlsx := f.workSheetReader(sheet)
  311. axis = f.mergeCellsParser(xlsx, axis)
  312. linkTypes := map[string]xlsxHyperlink{
  313. "External": {},
  314. "Location": {Location: link},
  315. }
  316. hyperlink, ok := linkTypes[linkType]
  317. if !ok || axis == "" {
  318. return
  319. }
  320. hyperlink.Ref = axis
  321. if linkType == "External" {
  322. rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, linkType)
  323. hyperlink.RID = "rId" + strconv.Itoa(rID)
  324. }
  325. if xlsx.Hyperlinks == nil {
  326. xlsx.Hyperlinks = &xlsxHyperlinks{}
  327. }
  328. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
  329. }
  330. // GetCellHyperLink provides a function to get cell hyperlink by given
  331. // worksheet name and axis. Boolean type value link will be ture if the cell
  332. // has a hyperlink and the target is the address of the hyperlink. Otherwise,
  333. // the value of link will be false and the value of the target will be a blank
  334. // string. For example get hyperlink of Sheet1!H6:
  335. //
  336. // link, target := xlsx.GetCellHyperLink("Sheet1", "H6")
  337. //
  338. func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) {
  339. var link bool
  340. var target string
  341. xlsx := f.workSheetReader(sheet)
  342. axis = f.mergeCellsParser(xlsx, axis)
  343. if xlsx.Hyperlinks == nil || axis == "" {
  344. return link, target
  345. }
  346. for h := range xlsx.Hyperlinks.Hyperlink {
  347. if xlsx.Hyperlinks.Hyperlink[h].Ref == axis {
  348. link = true
  349. target = xlsx.Hyperlinks.Hyperlink[h].Location
  350. if xlsx.Hyperlinks.Hyperlink[h].RID != "" {
  351. target = f.getSheetRelationshipsTargetByID(sheet, xlsx.Hyperlinks.Hyperlink[h].RID)
  352. }
  353. }
  354. }
  355. return link, target
  356. }
  357. // MergeCell provides a function to merge cells by given coordinate area and
  358. // sheet name. For example create a merged cell of D3:E9 on Sheet1:
  359. //
  360. // xlsx.MergeCell("Sheet1", "D3", "E9")
  361. //
  362. // If you create a merged cell that overlaps with another existing merged cell,
  363. // those merged cells that already exist will be removed.
  364. func (f *File) MergeCell(sheet, hcell, vcell string) {
  365. if hcell == vcell {
  366. return
  367. }
  368. hcell = strings.ToUpper(hcell)
  369. vcell = strings.ToUpper(vcell)
  370. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  371. hcol := string(strings.Map(letterOnlyMapF, hcell))
  372. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  373. hyAxis := hrow - 1
  374. hxAxis := TitleToNumber(hcol)
  375. vcol := string(strings.Map(letterOnlyMapF, vcell))
  376. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  377. vyAxis := vrow - 1
  378. vxAxis := TitleToNumber(vcol)
  379. if vxAxis < hxAxis {
  380. hcell, vcell = vcell, hcell
  381. vxAxis, hxAxis = hxAxis, vxAxis
  382. }
  383. if vyAxis < hyAxis {
  384. hcell, vcell = vcell, hcell
  385. vyAxis, hyAxis = hyAxis, vyAxis
  386. }
  387. xlsx := f.workSheetReader(sheet)
  388. if xlsx.MergeCells != nil {
  389. mergeCell := xlsxMergeCell{}
  390. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  391. mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
  392. // Delete the merged cells of the overlapping area.
  393. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  394. if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) {
  395. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  396. } else if checkCellInArea(vcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[1], mergeCell.Ref) {
  397. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  398. }
  399. }
  400. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells, &mergeCell)
  401. } else {
  402. mergeCell := xlsxMergeCell{}
  403. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  404. mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
  405. mergeCells := xlsxMergeCells{}
  406. mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
  407. xlsx.MergeCells = &mergeCells
  408. }
  409. }
  410. // SetCellInt provides a function to set int type value of a cell by given
  411. // worksheet name, cell coordinates and cell value.
  412. func (f *File) SetCellInt(sheet, axis string, value int) {
  413. xlsx := f.workSheetReader(sheet)
  414. axis = f.mergeCellsParser(xlsx, axis)
  415. col := string(strings.Map(letterOnlyMapF, axis))
  416. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  417. if err != nil {
  418. return
  419. }
  420. xAxis := row - 1
  421. yAxis := TitleToNumber(col)
  422. rows := xAxis + 1
  423. cell := yAxis + 1
  424. completeRow(xlsx, rows, cell)
  425. completeCol(xlsx, rows, cell)
  426. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  427. xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
  428. xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
  429. }
  430. // prepareCellStyle provides a function to prepare style index of cell in
  431. // worksheet by given column index and style index.
  432. func (f *File) prepareCellStyle(xlsx *xlsxWorksheet, col, style int) int {
  433. if xlsx.Cols != nil && style == 0 {
  434. for _, v := range xlsx.Cols.Col {
  435. if v.Min <= col && col <= v.Max {
  436. style = v.Style
  437. }
  438. }
  439. }
  440. return style
  441. }
  442. // SetCellStr provides a function to set string type value of a cell. Total
  443. // number of characters that a cell can contain 32767 characters.
  444. func (f *File) SetCellStr(sheet, axis, value string) {
  445. xlsx := f.workSheetReader(sheet)
  446. axis = f.mergeCellsParser(xlsx, axis)
  447. if len(value) > 32767 {
  448. value = value[0:32767]
  449. }
  450. col := string(strings.Map(letterOnlyMapF, axis))
  451. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  452. if err != nil {
  453. return
  454. }
  455. xAxis := row - 1
  456. yAxis := TitleToNumber(col)
  457. rows := xAxis + 1
  458. cell := yAxis + 1
  459. completeRow(xlsx, rows, cell)
  460. completeCol(xlsx, rows, cell)
  461. // Leading space(s) character detection.
  462. if len(value) > 0 {
  463. if value[0] == 32 {
  464. xlsx.SheetData.Row[xAxis].C[yAxis].XMLSpace = xml.Attr{
  465. Name: xml.Name{Space: NameSpaceXML, Local: "space"},
  466. Value: "preserve",
  467. }
  468. }
  469. }
  470. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  471. xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
  472. xlsx.SheetData.Row[xAxis].C[yAxis].V = value
  473. }
  474. // SetCellDefault provides a function to set string type value of a cell as
  475. // default format without escaping the cell.
  476. func (f *File) SetCellDefault(sheet, axis, value string) {
  477. xlsx := f.workSheetReader(sheet)
  478. axis = f.mergeCellsParser(xlsx, axis)
  479. col := string(strings.Map(letterOnlyMapF, axis))
  480. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  481. if err != nil {
  482. return
  483. }
  484. xAxis := row - 1
  485. yAxis := TitleToNumber(col)
  486. rows := xAxis + 1
  487. cell := yAxis + 1
  488. completeRow(xlsx, rows, cell)
  489. completeCol(xlsx, rows, cell)
  490. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  491. xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
  492. xlsx.SheetData.Row[xAxis].C[yAxis].V = value
  493. }
  494. // SetSheetRow writes an array to row by given worksheet name, starting
  495. // coordinate and a pointer to array type 'slice'. For example, writes an
  496. // array to row 6 start with the cell B6 on Sheet1:
  497. //
  498. // xlsx.SetSheetRow("Sheet1", "B6", &[]interface{}{"1", nil, 2})
  499. //
  500. func (f *File) SetSheetRow(sheet, axis string, slice interface{}) {
  501. xlsx := f.workSheetReader(sheet)
  502. axis = f.mergeCellsParser(xlsx, axis)
  503. col := string(strings.Map(letterOnlyMapF, axis))
  504. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  505. if err != nil {
  506. return
  507. }
  508. // Make sure 'slice' is a Ptr to Slice
  509. v := reflect.ValueOf(slice)
  510. if v.Kind() != reflect.Ptr {
  511. return
  512. }
  513. v = v.Elem()
  514. if v.Kind() != reflect.Slice {
  515. return
  516. }
  517. xAxis := row - 1
  518. yAxis := TitleToNumber(col)
  519. rows := xAxis + 1
  520. cell := yAxis + 1
  521. completeRow(xlsx, rows, cell)
  522. completeCol(xlsx, rows, cell)
  523. idx := 0
  524. for i := cell - 1; i < v.Len()+cell-1; i++ {
  525. c := ToAlphaString(i) + strconv.Itoa(row)
  526. f.SetCellValue(sheet, c, v.Index(idx).Interface())
  527. idx++
  528. }
  529. }
  530. // checkCellInArea provides a function to determine if a given coordinate is
  531. // within an area.
  532. func checkCellInArea(cell, area string) bool {
  533. cell = strings.ToUpper(cell)
  534. area = strings.ToUpper(area)
  535. ref := strings.Split(area, ":")
  536. if len(ref) < 2 {
  537. return false
  538. }
  539. from := ref[0]
  540. to := ref[1]
  541. col, row := getCellColRow(cell)
  542. fromCol, fromRow := getCellColRow(from)
  543. toCol, toRow := getCellColRow(to)
  544. return axisLowerOrEqualThan(fromCol, col) && axisLowerOrEqualThan(col, toCol) && axisLowerOrEqualThan(fromRow, row) && axisLowerOrEqualThan(row, toRow)
  545. }