From 37c4575835a40126ec7ce227c18aa8dbf9cc4bf3 Mon Sep 17 00:00:00 2001 From: ahmad Date: Mon, 31 Oct 2016 13:13:22 +0200 Subject: looping on row col functionality --- rows.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ sheet.go | 16 ++++++++++++++++ test/Workbook1.xlsx | Bin 18311 -> 26846 bytes test/Workbook_2.xlsx | Bin 0 -> 26846 bytes test/Workbook_3.xlsx | Bin 0 -> 6879 bytes 5 files changed, 66 insertions(+) create mode 100644 rows.go create mode 100644 test/Workbook_2.xlsx create mode 100644 test/Workbook_3.xlsx diff --git a/rows.go b/rows.go new file mode 100644 index 0000000..c717c23 --- /dev/null +++ b/rows.go @@ -0,0 +1,50 @@ +package excelize + +import ( + "encoding/xml" + "strings" + "strconv" +) + + +// GetRows return all the rows in a sheet +func (f *File) GetRows(sheet string) ([]xlsxRow, error) { + var xlsx xlsxWorksheet + name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` + err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + if ( err != nil ) { + return nil, err + } + rows := xlsx.SheetData.Row + + return rows, nil + +} + + +// readXMLSST read xmlSST simple function +func readXMLSST(f *File) (xlsxSST, error) { + shardStrings := xlsxSST{} + err := xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings) + return shardStrings, err +} + +// 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 +func (self* xlsxC) GetValueFrom(f *File) (string, error) { + switch self.T { + case "s": + xlsxSI := 0 + xlsxSI, _ = strconv.Atoi(self.V) + d, err := readXMLSST(f) + if ( err != nil ) { + return "", err + } + return d.SI[xlsxSI].T, nil + case "str": + return self.V, nil + default: + return self.V, nil + } // switch +} diff --git a/sheet.go b/sheet.go index c0e9287..5315d70 100644 --- a/sheet.go +++ b/sheet.go @@ -185,6 +185,22 @@ func (f *File) SetActiveSheet(index int) { return } + + +func (f *File) GetColumnsLength(sheet string) (int, error) { + var xlsx xlsxWorksheet + name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` + err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + if ( err != nil ) { + return -1, err + } + rows := len(xlsx.SheetData.Row) + + return rows, nil + +} + + // Replace xl/workbook.xml XML tags to self-closing for compatible Office Excel 2007. func workBookCompatibility(workbookMarshal string) string { workbookMarshal = strings.Replace(workbookMarshal, `xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="`, `r:id="`, -1) diff --git a/test/Workbook1.xlsx b/test/Workbook1.xlsx index d2a9a2e..2ac442a 100644 Binary files a/test/Workbook1.xlsx and b/test/Workbook1.xlsx differ diff --git a/test/Workbook_2.xlsx b/test/Workbook_2.xlsx new file mode 100644 index 0000000..efb9785 Binary files /dev/null and b/test/Workbook_2.xlsx differ diff --git a/test/Workbook_3.xlsx b/test/Workbook_3.xlsx new file mode 100644 index 0000000..dd63d14 Binary files /dev/null and b/test/Workbook_3.xlsx differ -- cgit v1.2.1 From 1bbc54d4ec8d11ecb41bea99aa1b7220c4e6f040 Mon Sep 17 00:00:00 2001 From: ahmad Date: Mon, 31 Oct 2016 13:15:31 +0200 Subject: sheet re-arrange --- sheet.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/sheet.go b/sheet.go index 5315d70..c0e9287 100644 --- a/sheet.go +++ b/sheet.go @@ -185,22 +185,6 @@ func (f *File) SetActiveSheet(index int) { return } - - -func (f *File) GetColumnsLength(sheet string) (int, error) { - var xlsx xlsxWorksheet - name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` - err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx) - if ( err != nil ) { - return -1, err - } - rows := len(xlsx.SheetData.Row) - - return rows, nil - -} - - // Replace xl/workbook.xml XML tags to self-closing for compatible Office Excel 2007. func workBookCompatibility(workbookMarshal string) string { workbookMarshal = strings.Replace(workbookMarshal, `xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="`, `r:id="`, -1) -- cgit v1.2.1