rows.go 13.4 KB
Newer Older
xurime's avatar
xurime 已提交
1
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
xurime's avatar
xurime 已提交
2 3 4 5 6 7 8
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
// Package excelize providing a set of functions that allow you to write to
// and read from XLSX files. Support reads and writes XLSX file generated by
// Microsoft Excel™ 2007 and later. Support save file without losing original
// charts of XLSX. This library needs Go version 1.8 or later.
xurime's avatar
xurime 已提交
9

A
ahmad 已提交
10 11 12 13
package excelize

import (
	"encoding/xml"
L
Lunny Xiao 已提交
14
	"fmt"
15
	"math"
16
	"strconv"
A
ahmad 已提交
17 18
)

19 20
// GetRows return all the rows in a sheet by given worksheet name (case
// sensitive). For example:
21
//
xurime's avatar
xurime 已提交
22
//    rows, err := f.GetRows("Sheet1")
23
//    for _, row := range rows {
24 25 26 27 28 29
//        for _, colCell := range row {
//            fmt.Print(colCell, "\t")
//        }
//        fmt.Println()
//    }
//
30
func (f *File) GetRows(sheet string) ([][]string, error) {
H
Harris 已提交
31
	rows, err := f.Rows(sheet)
xurime's avatar
xurime 已提交
32 33 34
	if err != nil {
		return nil, err
	}
H
Harris 已提交
35 36 37
	results := make([][]string, 0, 64)
	for rows.Next() {
		if rows.Error() != nil {
38 39
			break
		}
H
Harris 已提交
40 41 42
		row, err := rows.Columns()
		if err != nil {
			break
43
		}
H
Harris 已提交
44
		results = append(results, row)
45
	}
H
Harris 已提交
46
	return results, nil
A
ahmad 已提交
47 48
}

L
Lunny Xiao 已提交
49 50
// Rows defines an iterator to a sheet
type Rows struct {
H
Harris 已提交
51 52 53 54
	err    error
	f      *File
	rows   []xlsxRow
	curRow int
L
Lunny Xiao 已提交
55 56 57 58
}

// Next will return true if find the next row element.
func (rows *Rows) Next() bool {
H
Harris 已提交
59
	return rows.curRow < len(rows.rows)
L
Lunny Xiao 已提交
60 61 62 63 64 65 66 67
}

// Error will return the error when the find next row element
func (rows *Rows) Error() error {
	return rows.err
}

// Columns return the current row's column values
68
func (rows *Rows) Columns() ([]string, error) {
H
Harris 已提交
69 70 71 72
	curRow := rows.rows[rows.curRow]
	rows.curRow++

	columns := make([]string, len(curRow.C))
L
Lunny Xiao 已提交
73
	d := rows.f.sharedStringsReader()
H
Harris 已提交
74
	for _, colCell := range curRow.C {
75 76 77 78
		col, _, err := CellNameToCoordinates(colCell.R)
		if err != nil {
			return columns, err
		}
L
Lunny Xiao 已提交
79
		val, _ := colCell.getValueFrom(rows.f, d)
80
		columns[col-1] = val
L
Lunny Xiao 已提交
81
	}
82
	return columns, nil
L
Lunny Xiao 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
}

// ErrSheetNotExist defines an error of sheet is not exist
type ErrSheetNotExist struct {
	SheetName string
}

func (err ErrSheetNotExist) Error() string {
	return fmt.Sprintf("Sheet %s is not exist", string(err.SheetName))
}

// Rows return a rows iterator. For example:
//
xurime's avatar
xurime 已提交
96
//    rows, err := f.Rows("Sheet1")
L
Lunny Xiao 已提交
97
//    for rows.Next() {
xurime's avatar
xurime 已提交
98
//        row, err := rows.Columns()
99
//        for _, colCell := range row {
L
Lunny Xiao 已提交
100 101 102 103 104 105
//            fmt.Print(colCell, "\t")
//        }
//        fmt.Println()
//    }
//
func (f *File) Rows(sheet string) (*Rows, error) {
xurime's avatar
xurime 已提交
106 107 108 109
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return nil, err
	}
L
Lunny Xiao 已提交
110 111 112 113 114
	name, ok := f.sheetMap[trimSheetName(sheet)]
	if !ok {
		return nil, ErrSheetNotExist{sheet}
	}
	if xlsx != nil {
H
Harris 已提交
115 116
		data := f.readXML(name)
		f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpaceBytes(namespaceStrictToTransitional(data)))
L
Lunny Xiao 已提交
117 118
	}
	return &Rows{
H
Harris 已提交
119 120
		f:    f,
		rows: xlsx.SheetData.Row,
L
Lunny Xiao 已提交
121 122 123
	}, nil
}

124 125
// SetRowHeight provides a function to set the height of a single row. For
// example, set the height of the first row in Sheet1:
N
Nikolas Silva 已提交
126
//
xurime's avatar
xurime 已提交
127
//    err := f.SetRowHeight("Sheet1", 1, 50)
N
Nikolas Silva 已提交
128
//
129
func (f *File) SetRowHeight(sheet string, row int, height float64) error {
130
	if row < 1 {
131
		return newInvalidRowNumberError(row)
132
	}
133

xurime's avatar
xurime 已提交
134 135 136 137
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return err
	}
138 139 140

	prepareSheetXML(xlsx, 0, row)

141 142 143
	rowIdx := row - 1
	xlsx.SheetData.Row[rowIdx].Ht = height
	xlsx.SheetData.Row[rowIdx].CustomHeight = true
144
	return nil
N
Nikolas Silva 已提交
145 146
}

xurime's avatar
xurime 已提交
147
// getRowHeight provides a function to get row height in pixels by given sheet
148 149
// name and row index.
func (f *File) getRowHeight(sheet string, row int) int {
xurime's avatar
xurime 已提交
150
	xlsx, _ := f.workSheetReader(sheet)
151 152 153 154 155 156 157 158 159
	for _, v := range xlsx.SheetData.Row {
		if v.R == row+1 && v.Ht != 0 {
			return int(convertRowHeightToPixels(v.Ht))
		}
	}
	// Optimisation for when the row heights haven't changed.
	return int(defaultRowHeightPixels)
}

xurime's avatar
xurime 已提交
160
// GetRowHeight provides a function to get row height by given worksheet name
161 162
// and row index. For example, get the height of the first row in Sheet1:
//
xurime's avatar
xurime 已提交
163
//    height, err := f.GetRowHeight("Sheet1", 1)
164
//
165
func (f *File) GetRowHeight(sheet string, row int) (float64, error) {
166
	if row < 1 {
167
		return defaultRowHeightPixels, newInvalidRowNumberError(row)
168 169
	}

xurime's avatar
xurime 已提交
170 171 172 173
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return defaultRowHeightPixels, err
	}
174
	if row > len(xlsx.SheetData.Row) {
175
		return defaultRowHeightPixels, nil // it will be better to use 0, but we take care with BC
176
	}
177
	for _, v := range xlsx.SheetData.Row {
178
		if v.R == row && v.Ht != 0 {
179
			return v.Ht, nil
180 181 182
		}
	}
	// Optimisation for when the row heights haven't changed.
183
	return defaultRowHeightPixels, nil
184 185
}

xurime's avatar
xurime 已提交
186
// sharedStringsReader provides a function to get the pointer to the structure
187 188 189 190
// after deserialization of xl/sharedStrings.xml.
func (f *File) sharedStringsReader() *xlsxSST {
	if f.SharedStrings == nil {
		var sharedStrings xlsxSST
191
		ss := f.readXML("xl/sharedStrings.xml")
192
		if len(ss) == 0 {
193 194
			ss = f.readXML("xl/SharedStrings.xml")
		}
195
		_ = xml.Unmarshal(namespaceStrictToTransitional(ss), &sharedStrings)
196 197 198
		f.SharedStrings = &sharedStrings
	}
	return f.SharedStrings
A
ahmad 已提交
199 200
}

xurime's avatar
xurime 已提交
201 202 203
// getValueFrom return a value from a column/row cell, this function is
// inteded to be used with for range on rows an argument with the xlsx opened
// file.
204
func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
205 206 207 208
	switch xlsx.T {
	case "s":
		xlsxSI := 0
		xlsxSI, _ = strconv.Atoi(xlsx.V)
xurime's avatar
xurime 已提交
209 210 211 212 213 214 215
		if len(d.SI[xlsxSI].R) > 0 {
			value := ""
			for _, v := range d.SI[xlsxSI].R {
				value += v.T
			}
			return value, nil
		}
216
		return f.formattedValue(xlsx.S, d.SI[xlsxSI].T), nil
217
	case "str":
218
		return f.formattedValue(xlsx.S, xlsx.V), nil
219 220
	case "inlineStr":
		return f.formattedValue(xlsx.S, xlsx.IS.T), nil
221
	default:
222
		return f.formattedValue(xlsx.S, xlsx.V), nil
223
	}
A
ahmad 已提交
224
}
225

226
// SetRowVisible provides a function to set visible of a single row by given
227
// worksheet name and Excel row number. For example, hide row 2 in Sheet1:
228
//
xurime's avatar
xurime 已提交
229
//    err := f.SetRowVisible("Sheet1", 2, false)
230
//
231
func (f *File) SetRowVisible(sheet string, row int, visible bool) error {
232
	if row < 1 {
233
		return newInvalidRowNumberError(row)
234
	}
235

xurime's avatar
xurime 已提交
236 237 238 239
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return err
	}
240 241
	prepareSheetXML(xlsx, 0, row)
	xlsx.SheetData.Row[row-1].Hidden = !visible
242
	return nil
243 244
}

245 246 247
// GetRowVisible provides a function to get visible of a single row by given
// worksheet name and Excel row number. For example, get visible state of row
// 2 in Sheet1:
248
//
xurime's avatar
xurime 已提交
249
//    visible, err := f.GetRowVisible("Sheet1", 2)
250
//
251
func (f *File) GetRowVisible(sheet string, row int) (bool, error) {
252
	if row < 1 {
253
		return false, newInvalidRowNumberError(row)
254 255
	}

xurime's avatar
xurime 已提交
256 257 258 259
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return false, err
	}
260
	if row > len(xlsx.SheetData.Row) {
261
		return false, nil
262
	}
263
	return !xlsx.SheetData.Row[row-1].Hidden, nil
264
}
265

266
// SetRowOutlineLevel provides a function to set outline level number of a
xurime's avatar
xurime 已提交
267 268
// single row by given worksheet name and Excel row number. For example,
// outline row 2 in Sheet1 to level 1:
269
//
xurime's avatar
xurime 已提交
270
//    err := f.SetRowOutlineLevel("Sheet1", 2, 1)
271
//
272
func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error {
273
	if row < 1 {
274
		return newInvalidRowNumberError(row)
275
	}
xurime's avatar
xurime 已提交
276 277 278 279
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return err
	}
280
	prepareSheetXML(xlsx, 0, row)
281
	xlsx.SheetData.Row[row-1].OutlineLevel = level
282
	return nil
283 284
}

xurime's avatar
xurime 已提交
285
// GetRowOutlineLevel provides a function to get outline level number of a
xurime's avatar
xurime 已提交
286 287
// single row by given worksheet name and Excel row number. For example, get
// outline number of row 2 in Sheet1:
288
//
xurime's avatar
xurime 已提交
289
//    level, err := f.GetRowOutlineLevel("Sheet1", 2)
290
//
291
func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error) {
292
	if row < 1 {
293
		return 0, newInvalidRowNumberError(row)
294
	}
xurime's avatar
xurime 已提交
295 296 297 298
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return 0, err
	}
299
	if row > len(xlsx.SheetData.Row) {
300
		return 0, nil
301
	}
302
	return xlsx.SheetData.Row[row-1].OutlineLevel, nil
303 304
}

C
caozhiyi 已提交
305
// RemoveRow provides a function to remove single row by given worksheet name
306
// and Excel row number. For example, remove row 3 in Sheet1:
307
//
xurime's avatar
xurime 已提交
308
//    err := f.RemoveRow("Sheet1", 3)
309
//
310 311 312 313
// Use this method with caution, which will affect changes in references such
// as formulas, charts, and so on. If there is any referenced value of the
// worksheet, it will cause a file error when you open it. The excelize only
// partially updates these references currently.
314
func (f *File) RemoveRow(sheet string, row int) error {
315
	if row < 1 {
316
		return newInvalidRowNumberError(row)
317 318
	}

xurime's avatar
xurime 已提交
319 320 321 322
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return err
	}
323
	if row > len(xlsx.SheetData.Row) {
324
		return f.adjustHelper(sheet, rows, row, -1)
325
	}
326 327 328 329
	for rowIdx := range xlsx.SheetData.Row {
		if xlsx.SheetData.Row[rowIdx].R == row {
			xlsx.SheetData.Row = append(xlsx.SheetData.Row[:rowIdx],
				xlsx.SheetData.Row[rowIdx+1:]...)[:len(xlsx.SheetData.Row)-1]
330
			return f.adjustHelper(sheet, rows, row, -1)
331 332
		}
	}
333
	return nil
334 335
}

336 337 338
// InsertRow provides a function to insert a new row after given Excel row
// number starting from 1. For example, create a new row before row 3 in
// Sheet1:
339
//
xurime's avatar
xurime 已提交
340
//    err := f.InsertRow("Sheet1", 3)
341
//
342 343 344 345
// Use this method with caution, which will affect changes in references such
// as formulas, charts, and so on. If there is any referenced value of the
// worksheet, it will cause a file error when you open it. The excelize only
// partially updates these references currently.
346
func (f *File) InsertRow(sheet string, row int) error {
347
	if row < 1 {
348
		return newInvalidRowNumberError(row)
349
	}
350
	return f.adjustHelper(sheet, rows, row, 1)
351 352
}

353
// DuplicateRow inserts a copy of specified row (by its Excel row number) below
V
Veniamin Albaev 已提交
354
//
xurime's avatar
xurime 已提交
355
//    err := f.DuplicateRow("Sheet1", 2)
V
Veniamin Albaev 已提交
356
//
357 358 359 360
// Use this method with caution, which will affect changes in references such
// as formulas, charts, and so on. If there is any referenced value of the
// worksheet, it will cause a file error when you open it. The excelize only
// partially updates these references currently.
361 362
func (f *File) DuplicateRow(sheet string, row int) error {
	return f.DuplicateRowTo(sheet, row, row+1)
363 364
}

365 366
// DuplicateRowTo inserts a copy of specified row by it Excel number
// to specified row position moving down exists rows after target position
367
//
xurime's avatar
xurime 已提交
368
//    err := f.DuplicateRowTo("Sheet1", 2, 7)
369
//
370 371 372 373
// Use this method with caution, which will affect changes in references such
// as formulas, charts, and so on. If there is any referenced value of the
// worksheet, it will cause a file error when you open it. The excelize only
// partially updates these references currently.
374
func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {
375
	if row < 1 {
376
		return newInvalidRowNumberError(row)
377
	}
378

xurime's avatar
xurime 已提交
379 380 381 382
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return err
	}
383
	if row > len(xlsx.SheetData.Row) || row2 < 1 || row == row2 {
384
		return nil
V
Veniamin Albaev 已提交
385 386
	}

387 388 389
	var ok bool
	var rowCopy xlsxRow

390
	for i, r := range xlsx.SheetData.Row {
V
Veniamin Albaev 已提交
391
		if r.R == row {
392
			rowCopy = xlsx.SheetData.Row[i]
393
			ok = true
V
Veniamin Albaev 已提交
394 395 396
			break
		}
	}
397
	if !ok {
398
		return nil
399
	}
V
Veniamin Albaev 已提交
400

401 402 403
	if err := f.adjustHelper(sheet, rows, row2, 1); err != nil {
		return err
	}
404 405

	idx2 := -1
406
	for i, r := range xlsx.SheetData.Row {
407 408 409 410 411
		if r.R == row2 {
			idx2 = i
			break
		}
	}
412
	if idx2 == -1 && len(xlsx.SheetData.Row) >= row2 {
413
		return nil
V
Veniamin Albaev 已提交
414
	}
415 416 417 418

	rowCopy.C = append(make([]xlsxC, 0, len(rowCopy.C)), rowCopy.C...)
	f.ajustSingleRowDimensions(&rowCopy, row2)

V
Veniamin Albaev 已提交
419
	if idx2 != -1 {
420
		xlsx.SheetData.Row[idx2] = rowCopy
V
Veniamin Albaev 已提交
421
	} else {
422
		xlsx.SheetData.Row = append(xlsx.SheetData.Row, rowCopy)
V
Veniamin Albaev 已提交
423
	}
424
	return nil
V
Veniamin Albaev 已提交
425 426
}

xurime's avatar
xurime 已提交
427 428
// checkRow provides a function to check and fill each column element for all
// rows and make that is continuous in a worksheet of XML. For example:
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
//
//    <row r="15" spans="1:22" x14ac:dyDescent="0.2">
//        <c r="A15" s="2" />
//        <c r="B15" s="2" />
//        <c r="F15" s="1" />
//        <c r="G15" s="1" />
//    </row>
//
// in this case, we should to change it to
//
//    <row r="15" spans="1:22" x14ac:dyDescent="0.2">
//        <c r="A15" s="2" />
//        <c r="B15" s="2" />
//        <c r="C15" s="2" />
//        <c r="D15" s="2" />
//        <c r="E15" s="2" />
//        <c r="F15" s="1" />
//        <c r="G15" s="1" />
//    </row>
//
// Noteice: this method could be very slow for large spreadsheets (more than
// 3000 rows one sheet).
451
func checkRow(xlsx *xlsxWorksheet) error {
452 453
	for rowIdx := range xlsx.SheetData.Row {
		rowData := &xlsx.SheetData.Row[rowIdx]
454

455 456 457
		colCount := len(rowData.C)
		if colCount == 0 {
			continue
458
		}
459 460 461 462
		lastCol, _, err := CellNameToCoordinates(rowData.C[colCount-1].R)
		if err != nil {
			return err
		}
463 464 465 466 467 468 469 470

		if colCount < lastCol {
			oldList := rowData.C
			newlist := make([]xlsxC, 0, lastCol)

			rowData.C = xlsx.SheetData.Row[rowIdx].C[:0]

			for colIdx := 0; colIdx < lastCol; colIdx++ {
471 472 473 474 475
				cellName, err := CoordinatesToCellName(colIdx+1, rowIdx+1)
				if err != nil {
					return err
				}
				newlist = append(newlist, xlsxC{R: cellName})
476 477 478 479 480 481
			}

			rowData.C = newlist

			for colIdx := range oldList {
				colData := &oldList[colIdx]
482 483 484 485
				colNum, _, err := CellNameToCoordinates(colData.R)
				if err != nil {
					return err
				}
486
				xlsx.SheetData.Row[rowIdx].C[colNum-1] = *colData
487 488 489
			}
		}
	}
490
	return nil
491 492
}

xurime's avatar
xurime 已提交
493 494 495
// convertRowHeightToPixels provides a function to convert the height of a
// cell from user's units to pixels. If the height hasn't been set by the user
// we use the default value. If the row is hidden it has a value of zero.
496 497 498 499 500 501 502 503
func convertRowHeightToPixels(height float64) float64 {
	var pixels float64
	if height == 0 {
		return pixels
	}
	pixels = math.Ceil(4.0 / 3.0 * height)
	return pixels
}