diff --git a/cell.go b/cell.go index 4ce9619e..6788daf2 100644 --- a/cell.go +++ b/cell.go @@ -6,14 +6,14 @@ import ( "strings" ) -// Get value from cell by given sheet index and axis in XLSX file +// GetCellValue provide function get value from cell by given sheet index and axis in XLSX file func GetCellValue(file []FileList, sheet string, axis string) string { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet row := getRowIndex(axis) xAxis := row - 1 name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` - xml.Unmarshal([]byte(readXml(file, name)), &xlsx) + xml.Unmarshal([]byte(readXML(file, name)), &xlsx) rows := len(xlsx.SheetData.Row) if rows <= xAxis { return `` @@ -26,7 +26,7 @@ func GetCellValue(file []FileList, sheet string, axis string) string { shardStrings := xlsxSST{} xlsxSI := 0 xlsxSI, _ = strconv.Atoi(v.V) - xml.Unmarshal([]byte(readXml(file, `xl/sharedStrings.xml`)), &shardStrings) + xml.Unmarshal([]byte(readXML(file, `xl/sharedStrings.xml`)), &shardStrings) return shardStrings.SI[xlsxSI].T case "str": return v.V diff --git a/excelize.go b/excelize.go index 9ee5e7c2..ba474cfc 100644 --- a/excelize.go +++ b/excelize.go @@ -8,12 +8,13 @@ import ( "strings" ) +// FileList define a populated xlsx.File struct. type FileList struct { Key string Value string } -// OpenFile() take the name of an XLSX file and returns a populated +// OpenFile take the name of an XLSX file and returns a populated // xlsx.File struct for it. func OpenFile(filename string) (file []FileList, err error) { var f *zip.ReadCloser @@ -25,7 +26,7 @@ func OpenFile(filename string) (file []FileList, err error) { return } -// Set int type value of a cell +// SetCellInt provide function to set int type value of a cell func SetCellInt(file []FileList, sheet string, axis string, value int) []FileList { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet @@ -35,7 +36,7 @@ func SetCellInt(file []FileList, sheet string, axis string, value int) []FileLis yAxis := titleToNumber(col) name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` - xml.Unmarshal([]byte(readXml(file, name)), &xlsx) + xml.Unmarshal([]byte(readXML(file, name)), &xlsx) rows := xAxis + 1 cell := yAxis + 1 @@ -56,7 +57,7 @@ func SetCellInt(file []FileList, sheet string, axis string, value int) []FileLis return file } -// Set string type value of a cell +// SetCellStr provide function to set string type value of a cell func SetCellStr(file []FileList, sheet string, axis string, value string) []FileList { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet @@ -66,7 +67,7 @@ func SetCellStr(file []FileList, sheet string, axis string, value string) []File yAxis := titleToNumber(col) name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` - xml.Unmarshal([]byte(readXml(file, name)), &xlsx) + xml.Unmarshal([]byte(readXML(file, name)), &xlsx) rows := xAxis + 1 cell := yAxis + 1 diff --git a/file.go b/file.go index d7123d0c..d30ffa21 100644 --- a/file.go +++ b/file.go @@ -6,23 +6,20 @@ import ( "os" ) -// Create a new xlsx file -// +// CreateFile provide function to create new file by default template // For example: -// // xlsx := CreateFile() -// func CreateFile() []FileList { var file []FileList - file = saveFileList(file, `_rels/.rels`, TEMPLATE_RELS) - file = saveFileList(file, `docProps/app.xml`, TEMPLATE_DOCPROPS_APP) - file = saveFileList(file, `docProps/core.xml`, TEMPLATE_DOCPROPS_CORE) - file = saveFileList(file, `xl/_rels/workbook.xml.rels`, TEMPLATE_WORKBOOK_RELS) - file = saveFileList(file, `xl/theme/theme1.xml`, TEMPLATE_THEME) - file = saveFileList(file, `xl/worksheets/sheet1.xml`, TEMPLATE_SHEET) - file = saveFileList(file, `xl/styles.xml`, TEMPLATE_STYLES) - file = saveFileList(file, `xl/workbook.xml`, TEMPLATE_WORKBOOK) - file = saveFileList(file, `[Content_Types].xml`, TEMPLATE_CONTENT_TYPES) + file = saveFileList(file, `_rels/.rels`, templateRels) + file = saveFileList(file, `docProps/app.xml`, templateDocpropsApp) + file = saveFileList(file, `docProps/core.xml`, templateDocpropsCore) + file = saveFileList(file, `xl/_rels/workbook.xml.rels`, templateWorkbookRels) + file = saveFileList(file, `xl/theme/theme1.xml`, templateTheme) + file = saveFileList(file, `xl/worksheets/sheet1.xml`, templateSheet) + file = saveFileList(file, `xl/styles.xml`, templateStyles) + file = saveFileList(file, `xl/workbook.xml`, templateWorkbook) + file = saveFileList(file, `[Content_Types].xml`, templateContentTypes) return file } diff --git a/lib.go b/lib.go index 27b08e5f..93234118 100644 --- a/lib.go +++ b/lib.go @@ -11,15 +11,15 @@ import ( "strings" ) -// ReadZip() takes a pointer to a zip.ReadCloser and returns a -// xlsx.File struct populated with its contents. In most cases +// ReadZip takes a pointer to a zip.ReadCloser and returns a +// xlsx.File struct populated with its contents. In most cases // ReadZip is not used directly, but is called internally by OpenFile. func ReadZip(f *zip.ReadCloser) ([]FileList, error) { defer f.Close() return ReadZipReader(&f.Reader) } -// ReadZipReader() can be used to read an XLSX in memory without +// ReadZipReader can be used to read an XLSX in memory without // touching the filesystem. func ReadZipReader(r *zip.Reader) ([]FileList, error) { var fileList []FileList @@ -34,7 +34,7 @@ func ReadZipReader(r *zip.Reader) ([]FileList, error) { } // Read XML content as string and replace drawing property in XML namespace of sheet -func readXml(files []FileList, name string) string { +func readXML(files []FileList, name string) string { for _, file := range files { if file.Key == name { return strings.Replace(file.Value, " 0 { content.BookViews.WorkBookView[0].ActiveTab = index } else { @@ -146,12 +146,12 @@ func SetActiveSheet(file []FileList, index int) []FileList { fmt.Println(err) } file = saveFileList(file, `xl/workbook.xml`, workBookCompatibility(replaceRelationshipsNameSpace(string(output)))) - index += 1 + index++ for i := 0; i < sheets; i++ { xlsx := xlsxWorksheet{} sheetIndex := i + 1 path := `xl/worksheets/sheet` + strconv.Itoa(sheetIndex) + `.xml` - xml.Unmarshal([]byte(readXml(file, path)), &xlsx) + xml.Unmarshal([]byte(readXML(file, path)), &xlsx) if index == sheetIndex { if len(xlsx.SheetViews.SheetView) > 0 { xlsx.SheetViews.SheetView[0].TabSelected = true diff --git a/templates.go b/templates.go index 3d8e24fe..c6b748c0 100644 --- a/templates.go +++ b/templates.go @@ -3,14 +3,15 @@ package excelize +// XMLHeader define an XML declaration can also contain a standalone declaration const XMLHeader = "\n" -const TEMPLATE_DOCPROPS_APP = ` +const templateDocpropsApp = ` 0 Go XLSX ` -const TEMPLATE_CONTENT_TYPES = ` +const templateContentTypes = ` @@ -21,20 +22,20 @@ const TEMPLATE_CONTENT_TYPES = ` ` -const TEMPLATE_WORKBOOK = ` +const templateWorkbook = ` ` -const TEMPLATE_STYLES = ` +const templateStyles = ` ` -const TEMPLATE_SHEET = ` +const templateSheet = ` ` -const TEMPLATE_WORKBOOK_RELS = `` +const templateWorkbookRels = `` -const TEMPLATE_DOCPROPS_CORE = `xuri2006-09-16T00:00:00Z2006-09-16T00:00:00Z` +const templateDocpropsCore = `xuri2006-09-16T00:00:00Z2006-09-16T00:00:00Z` -const TEMPLATE_RELS = `` +const templateRels = `` -const TEMPLATE_THEME = ` +const templateTheme = ` ` diff --git a/xmlWorkbook.go b/xmlWorkbook.go index 3c63d85b..c9a04349 100644 --- a/xmlWorkbook.go +++ b/xmlWorkbook.go @@ -23,7 +23,7 @@ type xlsxWorkbookRels struct { // xmlxWorkbookRelation maps sheet id and xl/worksheets/sheet%d.xml type xlsxWorkbookRelation struct { - Id string `xml:",attr"` + ID string `xml:"Id,attr"` Target string `xml:",attr"` Type string `xml:",attr"` } @@ -118,8 +118,8 @@ type xlsxSheets struct { // as I need. type xlsxSheet struct { Name string `xml:"name,attr,omitempty"` - SheetId string `xml:"sheetId,attr,omitempty"` - Id string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"` + SheetID string `xml:"sheetId,attr,omitempty"` + ID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"` State string `xml:"state,attr,omitempty"` } @@ -161,7 +161,7 @@ type xlsxDefinedName struct { // currently I have not checked it for completeness - it does as much // as I need. type xlsxCalcPr struct { - CalcId string `xml:"calcId,attr,omitempty"` + CalcID string `xml:"calcId,attr,omitempty"` IterateCount int `xml:"iterateCount,attr,omitempty"` RefMode string `xml:"refMode,attr,omitempty"` Iterate bool `xml:"iterate,attr,omitempty"` diff --git a/xmlWorksheet.go b/xmlWorksheet.go index a5744c96..fb3639d1 100644 --- a/xmlWorksheet.go +++ b/xmlWorksheet.go @@ -28,7 +28,7 @@ type xlsxWorksheet struct { // xlsxDrawing change r:id to rid in the namespace type xlsxDrawing struct { - RId string `xml:"rid,attr"` + RID string `xml:"rid,attr"` } // xlsxHeaderFooter directly maps the headerFooter element in the namespace @@ -144,7 +144,7 @@ type xlsxSheetView struct { // ZoomScale float64 `xml:"zoomScale,attr"` // ZoomScaleNormal float64 `xml:"zoomScaleNormal,attr"` // ZoomScalePageLayoutView float64 `xml:"zoomScalePageLayoutView,attr"` - WorkbookViewId int `xml:"workbookViewId,attr"` + WorkbookViewID int `xml:"workbookViewId,attr"` Selection []xlsxSelection `xml:"selection"` Pane *xlsxPane `xml:"pane,omitempty"` } @@ -156,7 +156,7 @@ type xlsxSheetView struct { type xlsxSelection struct { Pane string `xml:"pane,attr,omitempty"` ActiveCell string `xml:"activeCell,attr"` - ActiveCellId int `xml:"activeCellId,attr"` + ActiveCellID int `xml:"activeCellId,attr"` SQRef string `xml:"sqref,attr"` }