cell.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 formula == "" {
  287. xlsx.SheetData.Row[xAxis].C[yAxis].F = nil
  288. f.deleteCalcChain(axis)
  289. return
  290. }
  291. if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
  292. xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
  293. } else {
  294. f := xlsxF{
  295. Content: formula,
  296. }
  297. xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
  298. }
  299. }
  300. // SetCellHyperLink provides a function to set cell hyperlink by given
  301. // worksheet name and link URL address. LinkType defines two types of
  302. // hyperlink "External" for web site or "Location" for moving to one of cell
  303. // in this workbook. The below is example for external link.
  304. //
  305. // xlsx.SetCellHyperLink("Sheet1", "A3", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  306. // // Set underline and font color style for the cell.
  307. // style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
  308. // xlsx.SetCellStyle("Sheet1", "A3", "A3", style)
  309. //
  310. // A this is another example for "Location":
  311. //
  312. // xlsx.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")
  313. //
  314. func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) {
  315. xlsx := f.workSheetReader(sheet)
  316. axis = f.mergeCellsParser(xlsx, axis)
  317. linkTypes := map[string]xlsxHyperlink{
  318. "External": {},
  319. "Location": {Location: link},
  320. }
  321. hyperlink, ok := linkTypes[linkType]
  322. if !ok || axis == "" {
  323. return
  324. }
  325. hyperlink.Ref = axis
  326. if linkType == "External" {
  327. rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, linkType)
  328. hyperlink.RID = "rId" + strconv.Itoa(rID)
  329. }
  330. if xlsx.Hyperlinks == nil {
  331. xlsx.Hyperlinks = &xlsxHyperlinks{}
  332. }
  333. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
  334. }
  335. // GetCellHyperLink provides a function to get cell hyperlink by given
  336. // worksheet name and axis. Boolean type value link will be ture if the cell
  337. // has a hyperlink and the target is the address of the hyperlink. Otherwise,
  338. // the value of link will be false and the value of the target will be a blank
  339. // string. For example get hyperlink of Sheet1!H6:
  340. //
  341. // link, target := xlsx.GetCellHyperLink("Sheet1", "H6")
  342. //
  343. func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) {
  344. var link bool
  345. var target string
  346. xlsx := f.workSheetReader(sheet)
  347. axis = f.mergeCellsParser(xlsx, axis)
  348. if xlsx.Hyperlinks == nil || axis == "" {
  349. return link, target
  350. }
  351. for h := range xlsx.Hyperlinks.Hyperlink {
  352. if xlsx.Hyperlinks.Hyperlink[h].Ref == axis {
  353. link = true
  354. target = xlsx.Hyperlinks.Hyperlink[h].Location
  355. if xlsx.Hyperlinks.Hyperlink[h].RID != "" {
  356. target = f.getSheetRelationshipsTargetByID(sheet, xlsx.Hyperlinks.Hyperlink[h].RID)
  357. }
  358. }
  359. }
  360. return link, target
  361. }
  362. // MergeCell provides a function to merge cells by given coordinate area and
  363. // sheet name. For example create a merged cell of D3:E9 on Sheet1:
  364. //
  365. // xlsx.MergeCell("Sheet1", "D3", "E9")
  366. //
  367. // If you create a merged cell that overlaps with another existing merged cell,
  368. // those merged cells that already exist will be removed.
  369. func (f *File) MergeCell(sheet, hcell, vcell string) {
  370. if hcell == vcell {
  371. return
  372. }
  373. hcell = strings.ToUpper(hcell)
  374. vcell = strings.ToUpper(vcell)
  375. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  376. hcol := string(strings.Map(letterOnlyMapF, hcell))
  377. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  378. hyAxis := hrow - 1
  379. hxAxis := TitleToNumber(hcol)
  380. vcol := string(strings.Map(letterOnlyMapF, vcell))
  381. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  382. vyAxis := vrow - 1
  383. vxAxis := TitleToNumber(vcol)
  384. if vxAxis < hxAxis {
  385. hcell, vcell = vcell, hcell
  386. vxAxis, hxAxis = hxAxis, vxAxis
  387. }
  388. if vyAxis < hyAxis {
  389. hcell, vcell = vcell, hcell
  390. vyAxis, hyAxis = hyAxis, vyAxis
  391. }
  392. xlsx := f.workSheetReader(sheet)
  393. if xlsx.MergeCells != nil {
  394. mergeCell := xlsxMergeCell{}
  395. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  396. mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
  397. // Delete the merged cells of the overlapping area.
  398. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  399. if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) {
  400. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  401. } else if checkCellInArea(vcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[1], mergeCell.Ref) {
  402. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  403. }
  404. }
  405. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells, &mergeCell)
  406. } else {
  407. mergeCell := xlsxMergeCell{}
  408. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  409. mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
  410. mergeCells := xlsxMergeCells{}
  411. mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
  412. xlsx.MergeCells = &mergeCells
  413. }
  414. }
  415. // SetCellInt provides a function to set int type value of a cell by given
  416. // worksheet name, cell coordinates and cell value.
  417. func (f *File) SetCellInt(sheet, axis string, value int) {
  418. xlsx := f.workSheetReader(sheet)
  419. axis = f.mergeCellsParser(xlsx, axis)
  420. col := string(strings.Map(letterOnlyMapF, axis))
  421. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  422. if err != nil {
  423. return
  424. }
  425. xAxis := row - 1
  426. yAxis := TitleToNumber(col)
  427. rows := xAxis + 1
  428. cell := yAxis + 1
  429. completeRow(xlsx, rows, cell)
  430. completeCol(xlsx, rows, cell)
  431. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  432. xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
  433. xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
  434. }
  435. // prepareCellStyle provides a function to prepare style index of cell in
  436. // worksheet by given column index and style index.
  437. func (f *File) prepareCellStyle(xlsx *xlsxWorksheet, col, style int) int {
  438. if xlsx.Cols != nil && style == 0 {
  439. for _, v := range xlsx.Cols.Col {
  440. if v.Min <= col && col <= v.Max {
  441. style = v.Style
  442. }
  443. }
  444. }
  445. return style
  446. }
  447. // SetCellStr provides a function to set string type value of a cell. Total
  448. // number of characters that a cell can contain 32767 characters.
  449. func (f *File) SetCellStr(sheet, axis, value string) {
  450. xlsx := f.workSheetReader(sheet)
  451. axis = f.mergeCellsParser(xlsx, axis)
  452. if len(value) > 32767 {
  453. value = value[0:32767]
  454. }
  455. col := string(strings.Map(letterOnlyMapF, axis))
  456. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  457. if err != nil {
  458. return
  459. }
  460. xAxis := row - 1
  461. yAxis := TitleToNumber(col)
  462. rows := xAxis + 1
  463. cell := yAxis + 1
  464. completeRow(xlsx, rows, cell)
  465. completeCol(xlsx, rows, cell)
  466. // Leading space(s) character detection.
  467. if len(value) > 0 {
  468. if value[0] == 32 {
  469. xlsx.SheetData.Row[xAxis].C[yAxis].XMLSpace = xml.Attr{
  470. Name: xml.Name{Space: NameSpaceXML, Local: "space"},
  471. Value: "preserve",
  472. }
  473. }
  474. }
  475. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  476. xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
  477. xlsx.SheetData.Row[xAxis].C[yAxis].V = value
  478. }
  479. // SetCellDefault provides a function to set string type value of a cell as
  480. // default format without escaping the cell.
  481. func (f *File) SetCellDefault(sheet, axis, value string) {
  482. xlsx := f.workSheetReader(sheet)
  483. axis = f.mergeCellsParser(xlsx, axis)
  484. col := string(strings.Map(letterOnlyMapF, axis))
  485. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  486. if err != nil {
  487. return
  488. }
  489. xAxis := row - 1
  490. yAxis := TitleToNumber(col)
  491. rows := xAxis + 1
  492. cell := yAxis + 1
  493. completeRow(xlsx, rows, cell)
  494. completeCol(xlsx, rows, cell)
  495. xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
  496. xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
  497. xlsx.SheetData.Row[xAxis].C[yAxis].V = value
  498. }
  499. // SetSheetRow writes an array to row by given worksheet name, starting
  500. // coordinate and a pointer to array type 'slice'. For example, writes an
  501. // array to row 6 start with the cell B6 on Sheet1:
  502. //
  503. // xlsx.SetSheetRow("Sheet1", "B6", &[]interface{}{"1", nil, 2})
  504. //
  505. func (f *File) SetSheetRow(sheet, axis string, slice interface{}) {
  506. xlsx := f.workSheetReader(sheet)
  507. axis = f.mergeCellsParser(xlsx, axis)
  508. col := string(strings.Map(letterOnlyMapF, axis))
  509. row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  510. if err != nil {
  511. return
  512. }
  513. // Make sure 'slice' is a Ptr to Slice
  514. v := reflect.ValueOf(slice)
  515. if v.Kind() != reflect.Ptr {
  516. return
  517. }
  518. v = v.Elem()
  519. if v.Kind() != reflect.Slice {
  520. return
  521. }
  522. xAxis := row - 1
  523. yAxis := TitleToNumber(col)
  524. rows := xAxis + 1
  525. cell := yAxis + 1
  526. completeRow(xlsx, rows, cell)
  527. completeCol(xlsx, rows, cell)
  528. idx := 0
  529. for i := cell - 1; i < v.Len()+cell-1; i++ {
  530. c := ToAlphaString(i) + strconv.Itoa(row)
  531. f.SetCellValue(sheet, c, v.Index(idx).Interface())
  532. idx++
  533. }
  534. }
  535. // checkCellInArea provides a function to determine if a given coordinate is
  536. // within an area.
  537. func checkCellInArea(cell, area string) bool {
  538. cell = strings.ToUpper(cell)
  539. area = strings.ToUpper(area)
  540. ref := strings.Split(area, ":")
  541. if len(ref) < 2 {
  542. return false
  543. }
  544. from := ref[0]
  545. to := ref[1]
  546. col, row := getCellColRow(cell)
  547. fromCol, fromRow := getCellColRow(from)
  548. toCol, toRow := getCellColRow(to)
  549. return axisLowerOrEqualThan(fromCol, col) && axisLowerOrEqualThan(col, toCol) && axisLowerOrEqualThan(fromRow, row) && axisLowerOrEqualThan(row, toRow)
  550. }