Bladeren bron

Documentation:
* Add information about return value of NewSheet()
* Minor documentation language fixes
Samples:
* Added sample go file for dumping a XLSX file to the console

Sebastian Willing 7 jaren geleden
bovenliggende
commit
ccdefb3b16
2 gewijzigde bestanden met toevoegingen van 52 en 6 verwijderingen
  1. 45 0
      samples/dumpXLSX.go
  2. 7 6
      sheet.go

+ 45 - 0
samples/dumpXLSX.go

@@ -0,0 +1,45 @@
+package main
+
+import (
+	"fmt"
+	"os"
+
+	"github.com/360EntSecGroup-Skylar/excelize"
+)
+
+func main() {
+	// Exit on missing filename
+	if len(os.Args) < 2 || os.Args[1] == "" {
+		fmt.Println("Syntax: dumpXLSX <filename.xlsx>")
+		os.Exit(1)
+	}
+
+	// Open file and panic on error
+	fmt.Println("Reading ", os.Args[1])
+	xlsx, err := excelize.OpenFile(os.Args[1])
+	if err != nil {
+		panic(err)
+	}
+
+	// Read all sheets in map
+	for i, sheet := range xlsx.GetSheetMap() {
+		//Output sheet header
+		fmt.Printf("----- %d. %s -----\n", i, sheet)
+
+		// Get rows
+		rows := xlsx.GetRows(sheet)
+		// Create a row number prefix pattern long enough to fit all row numbers
+		prefixPattern := fmt.Sprintf("%% %dd ", len(fmt.Sprintf("%d", len(rows))))
+
+		// Walk through rows
+		for j, row := range rows {
+			// Output row number as prefix
+			fmt.Printf(prefixPattern, j)
+			// Output row content
+			for _, cell := range row {
+				fmt.Print(cell, "\t")
+			}
+			fmt.Println()
+		}
+	}
+}

+ 7 - 6
sheet.go

@@ -12,9 +12,10 @@ import (
 	"unicode/utf8"
 )
 
-// NewSheet provides function to create a new sheet by given worksheet name,
-// when creating a new XLSX file, the default sheet will be create, when you
-// create a new file.
+// NewSheet provides function to create a new sheet by given worksheet name.
+// When creating a new XLSX file, the default sheet will be created.
+// Returns the number of sheets in the workbook (file) after appending the new
+// sheet.
 func (f *File) NewSheet(name string) int {
 	// Check if the worksheet already exists
 	if f.GetSheetIndex(name) != 0 {
@@ -202,8 +203,8 @@ func replaceRelationshipsNameSpaceBytes(workbookMarshal []byte) []byte {
 }
 
 // SetActiveSheet provides function to set default active worksheet of XLSX by
-// given index. Note that active index is different with the index that got by
-// function GetSheetMap, and it should be greater than 0 and less than total
+// given index. Note that active index is different from the index returned by
+// function GetSheetMap(). It should be greater than 0 and less than total
 // worksheet numbers.
 func (f *File) SetActiveSheet(index int) {
 	if index < 1 {
@@ -237,7 +238,7 @@ func (f *File) SetActiveSheet(index int) {
 	}
 }
 
-// GetActiveSheetIndex provides function to get active sheet of XLSX. If not
+// GetActiveSheetIndex provides function to get active sheet index of the XLSX. If not
 // found the active sheet will be return integer 0.
 func (f *File) GetActiveSheetIndex() int {
 	buffer := bytes.Buffer{}