Skip to content

Commit

Permalink
Format commants, break comments after 80 characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Jan 18, 2017
1 parent f05df2a commit 52796f6
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 346 deletions.
7 changes: 4 additions & 3 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
)

// GetCellValue provide function get value from cell by given sheet index and axis in XLSX file.
// The value of the merged cell is not available currently.
// GetCellValue provide function get value from cell by given sheet index and
// axis in XLSX file. The value of the merged cell is not available currently.
func (f *File) GetCellValue(sheet string, axis string) string {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
Expand Down Expand Up @@ -50,7 +50,8 @@ func (f *File) GetCellValue(sheet string, axis string) string {
return ""
}

// GetCellFormula provide function get formula from cell by given sheet index and axis in XLSX file.
// GetCellFormula provide function get formula from cell by given sheet index
// and axis in XLSX file.
func (f *File) GetCellFormula(sheet string, axis string) string {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
Expand Down
4 changes: 2 additions & 2 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
)

// SetColWidth provides function to set the width of a single column or multiple columns.
// For example:
// SetColWidth provides function to set the width of a single column or multiple
// columns. For example:
//
// xlsx := excelize.CreateFile()
// xlsx.SetColWidth("Sheet1", "A", "H", 20)
Expand Down
20 changes: 12 additions & 8 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"strings"
)

// File define a populated xlsx.File struct.
// File define a populated XLSX file struct.
type File struct {
XLSX map[string]string
Path string
SheetCount int
}

// OpenFile take the name of an XLSX file and returns a populated
// xlsx.File struct for it.
// OpenFile take the name of an XLSX file and returns a populated XLSX file
// struct for it.
func OpenFile(filename string) (*File, error) {
var f *zip.ReadCloser
var err error
Expand Down Expand Up @@ -110,7 +110,8 @@ func (f *File) SetCellStr(sheet string, axis string, value string) {
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}

// SetCellDefault provides function to set string type value of a cell as default format without escaping the cell.
// SetCellDefault provides function to set string type value of a cell as
// default format without escaping the cell.
func (f *File) SetCellDefault(sheet string, axis string, value string) {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
Expand Down Expand Up @@ -203,15 +204,16 @@ func completeRow(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
return xlsx
}

// Replace xl/worksheets/sheet%d.xml XML tags to self-closing for compatible Office Excel 2007.
// Replace xl/worksheets/sheet%d.xml XML tags to self-closing for compatible
// Office Excel 2007.
func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
oldXmlns := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
newXmlns := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">`
workbookMarshal = strings.Replace(workbookMarshal, oldXmlns, newXmlns, -1)
return workbookMarshal
}

// Check XML tags and fix discontinuous case, for example:
// Check XML tags and fix discontinuous case. For example:
//
// <row r="15" spans="1:22" x14ac:dyDescent="0.2">
// <c r="A15" s="2" />
Expand All @@ -232,7 +234,8 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
// <c r="G15" s="1" />
// </row>
//
// Noteice: this method could be very slow for large spreadsheets (more than 3000 rows one sheet).
// Noteice: this method could be very slow for large spreadsheets (more than
// 3000 rows one sheet).
func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row {
Expand Down Expand Up @@ -267,7 +270,8 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {

// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
// Office Excel 2007 and 2010. This function will be remove value tag when met a
// cell have a linked value. Reference https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating?forum=excel
// cell have a linked value. Reference
// https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating?forum=excel
//
// Notice: after open XLSX file Excel will be update linked value and generate
// new value and will prompt save file or not.
Expand Down
46 changes: 23 additions & 23 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ func TestOpenFile(t *testing.T) {
if err != nil {
t.Log(err)
}
}

func TestBrokenFile(t *testing.T) {
// Test write file with broken file struct.
f2 := File{}
err = f2.Save()
err := f2.Save()
if err != nil {
t.Log(err)
}
Expand All @@ -124,41 +126,39 @@ func TestOpenFile(t *testing.T) {
if err != nil {
t.Log(err)
}
}

func TestCreateFile(t *testing.T) {
// Test create a XLSX file.
f3 := CreateFile()
f3.NewSheet(2, "XLSXSheet2")
f3.NewSheet(3, "XLSXSheet3")
f3.SetCellInt("Sheet2", "A23", 56)
f3.SetCellStr("SHEET1", "B20", "42")
f3.SetActiveSheet(0)
// Test add picture to sheet.
err := f3.AddPicture("Sheet1", "H2", "K12", "./test/images/excel.gif")
// Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
f3, err := OpenFile("./test/badWorkbook.xlsx")
f3.SetActiveSheet(2)
if err != nil {
t.Log(err)
}
err = f3.AddPicture("Sheet1", "C2", "F12", "./test/images/excel.tif")

// Test open a XLSX file with given illegal path.
_, err = OpenFile("./test/Workbook.xlsx")
if err != nil {
t.Log(err)
}
err = f3.WriteTo("./test/Workbook_3.xlsx")
}

func TestCreateFile(t *testing.T) {
// Test create a XLSX file.
f4 := CreateFile()
f4.NewSheet(2, "XLSXSheet2")
f4.NewSheet(3, "XLSXSheet3")
f4.SetCellInt("Sheet2", "A23", 56)
f4.SetCellStr("SHEET1", "B20", "42")
f4.SetActiveSheet(0)
// Test add picture to sheet.
err := f4.AddPicture("Sheet1", "H2", "K12", "./test/images/excel.gif")
if err != nil {
t.Log(err)
}
}

func TestBrokenFile(t *testing.T) {
// Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
f4, err := OpenFile("./test/badWorkbook.xlsx")
f4.SetActiveSheet(2)
err = f4.AddPicture("Sheet1", "C2", "F12", "./test/images/excel.tif")
if err != nil {
t.Log(err)
}

// Test open a XLSX file with given illegal path.
_, err = OpenFile("./test/Workbook.xlsx")
err = f4.WriteTo("./test/Workbook_3.xlsx")
if err != nil {
t.Log(err)
}
Expand Down
13 changes: 8 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"os"
)

// CreateFile provide function to create new file by default template.
// For example:
// xlsx := CreateFile()
// CreateFile provides function to create new file by default template. For
// example:
//
// xlsx := CreateFile()
//
func CreateFile() *File {
file := make(map[string]string)
file["_rels/.rels"] = templateRels
Expand All @@ -25,7 +27,7 @@ func CreateFile() *File {
}
}

// Save provide function override the xlsx file with origin path.
// Save provides function override the xlsx file with origin path.
func (f *File) Save() error {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
Expand All @@ -51,7 +53,8 @@ func (f *File) Save() error {
return err
}

// WriteTo provide function create or update to an xlsx file at the provided path.
// WriteTo provides function to create or update to an xlsx file at the provided
// path.
func (f *File) WriteTo(name string) error {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
Expand Down
18 changes: 9 additions & 9 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
"math"
)

// 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.
// 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) (map[string]string, int, error) {
defer f.Close()
return ReadZipReader(&f.Reader)
}

// ReadZipReader can be used to read an XLSX in memory without
// touching the filesystem.
// ReadZipReader can be used to read an XLSX in memory without touching the
// filesystem.
func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
fileList := make(map[string]string)
worksheets := 0
Expand Down Expand Up @@ -88,8 +88,8 @@ func titleToNumber(s string) int {
return sum - 1
}

// letterOnlyMapF is used in conjunction with strings.Map to return
// only the characters A-Z and a-z in a string.
// letterOnlyMapF is used in conjunction with strings.Map to return only the
// characters A-Z and a-z in a string.
func letterOnlyMapF(rune rune) rune {
switch {
case 'A' <= rune && rune <= 'Z':
Expand All @@ -100,8 +100,8 @@ func letterOnlyMapF(rune rune) rune {
return -1
}

// intOnlyMapF is used in conjunction with strings.Map to return only
// the numeric portions of a string.
// intOnlyMapF is used in conjunction with strings.Map to return only the
// numeric portions of a string.
func intOnlyMapF(rune rune) rune {
if rune >= 48 && rune < 58 {
return rune
Expand Down
46 changes: 26 additions & 20 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"strings"
)

// AddPicture provide the method to add picture in a sheet by given xAxis, yAxis and file path.
// For example:
// AddPicture provide the method to add picture in a sheet by given xAxis, yAxis
// and file path. For example:
//
// xlsx := excelize.CreateFile()
// err := xlsx.AddPicture("Sheet1", "A2", "H9", "./image.jpg")
Expand Down Expand Up @@ -63,8 +63,9 @@ func (f *File) AddPicture(sheet string, xAxis string, yAxis string, picture stri
return err
}

// addSheetRelationships provides function to add xl/worksheets/_rels/sheet%d.xml.rels by given
// sheet name, relationship type and target.
// addSheetRelationships provides function to add
// xl/worksheets/_rels/sheet%d.xml.rels by given sheet name, relationship type
// and target.
func (f *File) addSheetRelationships(sheet string, relType string, target string) int {
var rels = "xl/worksheets/_rels/" + strings.ToLower(sheet) + ".xml.rels"
var sheetRels xlsxWorkbookRels
Expand Down Expand Up @@ -93,8 +94,8 @@ func (f *File) addSheetRelationships(sheet string, relType string, target string
return rID
}

// addSheetDrawing provides function to add drawing element to xl/worksheets/sheet%d.xml by
// given sheet name and relationship index.
// addSheetDrawing provides function to add drawing element to
// xl/worksheets/sheet%d.xml by given sheet name and relationship index.
func (f *File) addSheetDrawing(sheet string, rID int) {
var xlsx xlsxWorksheet
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
Expand All @@ -109,7 +110,8 @@ func (f *File) addSheetDrawing(sheet string, rID int) {
f.saveFileList(name, string(output))
}

// countDrawings provides function to get drawing files count storage in the folder xl/drawings.
// countDrawings provides function to get drawing files count storage in the
// folder xl/drawings.
func (f *File) countDrawings() int {
count := 0
for k := range f.XLSX {
Expand All @@ -120,10 +122,10 @@ func (f *File) countDrawings() int {
return count
}

// addDrawing provides function to add picture by given drawingXML, xAxis, yAxis, file name
// and relationship index. In order to solve the problem that the label structure is changed
// after serialization and deserialization, two different structures: decodeWsDr and encodeWsDr
// are defined.
// addDrawing provides function to add picture by given drawingXML, xAxis,
// yAxis, file name and relationship index. In order to solve the problem that
// the label structure is changed after serialization and deserialization, two
// different structures: decodeWsDr and encodeWsDr are defined.
func (f *File) addDrawing(drawingXML string, xAxis string, yAxis string, file string, rID int) {
xAxis = strings.ToUpper(xAxis)
fromCol := string(strings.Map(letterOnlyMapF, xAxis))
Expand Down Expand Up @@ -186,8 +188,9 @@ func (f *File) addDrawing(drawingXML string, xAxis string, yAxis string, file st
f.saveFileList(drawingXML, result)
}

// addDrawingRelationships provides function to add image part relationships in the file
// xl/drawings/_rels/drawing%d.xml.rels by given drawing index, relationship type and target.
// addDrawingRelationships provides function to add image part relationships in
// the file xl/drawings/_rels/drawing%d.xml.rels by given drawing index,
// relationship type and target.
func (f *File) addDrawingRelationships(index int, relType string, target string) int {
var rels = "xl/drawings/_rels/drawing" + strconv.Itoa(index) + ".xml.rels"
var drawingRels xlsxWorkbookRels
Expand Down Expand Up @@ -216,7 +219,8 @@ func (f *File) addDrawingRelationships(index int, relType string, target string)
return rID
}

// countMedia provides function to get media files count storage in the folder xl/media/image.
// countMedia provides function to get media files count storage in the folder
// xl/media/image.
func (f *File) countMedia() int {
count := 0
for k := range f.XLSX {
Expand All @@ -227,17 +231,18 @@ func (f *File) countMedia() int {
return count
}

// addMedia provides function to add picture into folder xl/media/image by given file
// name and extension name.
// addMedia provides function to add picture into folder xl/media/image by given
// file name and extension name.
func (f *File) addMedia(file string, ext string) {
count := f.countMedia()
dat, _ := ioutil.ReadFile(file)
media := "xl/media/image" + strconv.Itoa(count+1) + ext
f.XLSX[media] = string(dat)
}

// addDrawingContentTypePart provides function to add image part relationships in
// http://purl.oclc.org/ooxml/officeDocument/relationships/image and appropriate content type.
// addDrawingContentTypePart provides function to add image part relationships
// in http://purl.oclc.org/ooxml/officeDocument/relationships/image and
// appropriate content type.
func (f *File) addDrawingContentTypePart(index int) {
var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false, "tiff": false}
var content xlsxTypes
Expand Down Expand Up @@ -271,8 +276,9 @@ func (f *File) addDrawingContentTypePart(index int) {
f.saveFileList(`[Content_Types].xml`, string(output))
}

// getSheetRelationshipsTargetByID provides function to get Target attribute value
// in xl/worksheets/_rels/sheet%d.xml.rels by given sheet name and relationship index.
// getSheetRelationshipsTargetByID provides function to get Target attribute
// value in xl/worksheets/_rels/sheet%d.xml.rels by given sheet name and
// relationship index.
func (f *File) getSheetRelationshipsTargetByID(sheet string, rID string) string {
var rels = "xl/worksheets/_rels/" + strings.ToLower(sheet) + ".xml.rels"
var sheetRels xlsxWorkbookRels
Expand Down
5 changes: 2 additions & 3 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ func readXMLSST(f *File) (xlsxSST, error) {
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.
// 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 (xlsx *xlsxC) getValueFrom(f *File) (string, error) {
switch xlsx.T {
case "s":
Expand Down
Loading

0 comments on commit 52796f6

Please sign in to comment.