Skip to content

Commit

Permalink
Format code with golint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Sep 2, 2016
1 parent 0a0a36b commit 192af02
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 61 deletions.
6 changes: 3 additions & 3 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
23 changes: 10 additions & 13 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, "<drawing r:id=", "<drawing rid=", -1)
Expand Down
40 changes: 20 additions & 20 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"strings"
)

// Create a new sheet by given index, when creating a new XLSX file,
// the default sheet will be create, when you create a new file, you
// need to ensure that the index is continuous.
// NewSheet provice function to greate a new sheet by given index, when
// creating a new XLSX file, the default sheet will be create, when you
// create a new file, you need to ensure that the index is continuous.
func NewSheet(file []FileList, index int, name string) []FileList {
// Update docProps/app.xml
file = setAppXml(file)
file = setAppXML(file)
// Update [Content_Types].xml
file = setContentTypes(file, index)
// Create new sheet /xl/worksheets/sheet%d.xml
Expand All @@ -27,7 +27,7 @@ func NewSheet(file []FileList, index int, name string) []FileList {
// Read and update property of contents type of XLSX
func setContentTypes(file []FileList, index int) []FileList {
var content xlsxTypes
xml.Unmarshal([]byte(readXml(file, `[Content_Types].xml`)), &content)
xml.Unmarshal([]byte(readXML(file, `[Content_Types].xml`)), &content)
content.Overrides = append(content.Overrides, xlsxOverride{
PartName: `/xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`,
ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
Expand All @@ -44,7 +44,7 @@ func setSheet(file []FileList, index int) []FileList {
var xlsx xlsxWorksheet
xlsx.Dimension.Ref = "A1"
xlsx.SheetViews.SheetView = append(xlsx.SheetViews.SheetView, xlsxSheetView{
WorkbookViewId: 0,
WorkbookViewID: 0,
})
output, err := xml.MarshalIndent(xlsx, "", "")
if err != nil {
Expand All @@ -57,14 +57,14 @@ func setSheet(file []FileList, index int) []FileList {
// Update workbook property of XLSX
func setWorkbook(file []FileList, index int, name string) []FileList {
var content xlsxWorkbook
xml.Unmarshal([]byte(readXml(file, `xl/workbook.xml`)), &content)
xml.Unmarshal([]byte(readXML(file, `xl/workbook.xml`)), &content)

rels := readXlsxWorkbookRels(file)
rId := len(rels.Relationships)
rID := len(rels.Relationships)
content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{
Name: name,
SheetId: strconv.Itoa(index),
Id: "rId" + strconv.Itoa(rId),
SheetID: strconv.Itoa(index),
ID: "rId" + strconv.Itoa(rID),
})
output, err := xml.MarshalIndent(content, "", "")
if err != nil {
Expand All @@ -76,16 +76,16 @@ func setWorkbook(file []FileList, index int, name string) []FileList {
// Read and unmarshal workbook relationships of XLSX
func readXlsxWorkbookRels(file []FileList) xlsxWorkbookRels {
var content xlsxWorkbookRels
xml.Unmarshal([]byte(readXml(file, `xl/_rels/workbook.xml.rels`)), &content)
xml.Unmarshal([]byte(readXML(file, `xl/_rels/workbook.xml.rels`)), &content)
return content
}

// Update workbook relationships property of XLSX
func addXlsxWorkbookRels(file []FileList, sheet int) []FileList {
content := readXlsxWorkbookRels(file)
rId := len(content.Relationships) + 1
rID := len(content.Relationships) + 1
content.Relationships = append(content.Relationships, xlsxWorkbookRelation{
Id: "rId" + strconv.Itoa(rId),
ID: "rId" + strconv.Itoa(rID),
Target: `worksheets/sheet` + strconv.Itoa(sheet) + `.xml`,
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
})
Expand All @@ -97,8 +97,8 @@ func addXlsxWorkbookRels(file []FileList, sheet int) []FileList {
}

// Update docProps/app.xml file of XML
func setAppXml(file []FileList) []FileList {
return saveFileList(file, `docProps/app.xml`, TEMPLATE_DOCPROPS_APP)
func setAppXML(file []FileList) []FileList {
return saveFileList(file, `docProps/app.xml`, templateDocpropsApp)
}

// Some tools that read XLSX files have very strict requirements about
Expand All @@ -125,14 +125,14 @@ func replaceRelationshipsID(workbookMarshal string) string {
return strings.Replace(rids, `<drawing rid="`, `<drawing r:id="`, -1)
}

// Set default active sheet of XLSX by given index
// SetActiveSheet provide function to set default active sheet of XLSX by given index
func SetActiveSheet(file []FileList, index int) []FileList {
var content xlsxWorkbook
if index < 1 {
index = 1
}
index -= 1
xml.Unmarshal([]byte(readXml(file, `xl/workbook.xml`)), &content)
index--
xml.Unmarshal([]byte(readXML(file, `xl/workbook.xml`)), &content)
if len(content.BookViews.WorkBookView) > 0 {
content.BookViews.WorkBookView[0].ActiveTab = index
} else {
Expand All @@ -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
Expand Down
Loading

0 comments on commit 192af02

Please sign in to comment.