forked from IntelligenceX/fileconversion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ODS 2 Text.go
92 lines (72 loc) · 1.92 KB
/
ODS 2 Text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
File Name: ODS 2 Text.go
Copyright: 2019 Kleissner Investments s.r.o.
Author: Peter Kleissner
Code for parsing Open Document Spreadsheet files. ZIP-compressed XML-based file format.
*/
package fileconversion
import (
"io"
"github.com/IntelligenceX/fileconversion/odf/ods"
)
// ODS2Text extracts text of an OpenDocument Spreadsheet
// Size is the full size of the input file.
func ODS2Text(file io.ReaderAt, size int64, writer io.Writer, limit int64) (written int64, err error) {
var doc ods.Doc
f, err := ods.NewReader(file, size)
if err != nil {
return 0, err
}
defer f.Close()
if err := f.ParseContent(&doc); err != nil {
return 0, err
}
for n, sheet := range doc.Table {
rows := sheet.Strings()
if err = writeOutput(writer, []byte(xlGenerateSheetTitle(sheet.Name, n, int(len(rows)))), &written, &limit); err != nil || limit == 0 {
return written, err
}
for _, row := range rows {
rowText := ""
// go through all columns
for m, text := range row {
if text != "" {
text = cleanCell(text)
if m > 0 {
rowText += ", "
}
rowText += text
}
}
rowText += "\n"
if err = writeOutput(writer, []byte(rowText), &written, &limit); err != nil || limit == 0 {
return written, err
}
}
}
return written, nil
}
// ODS2Cells converts an ODS file to individual cells
// Size is the full size of the input file.
func ODS2Cells(file io.ReaderAt, size int64) (cells []string, err error) {
var doc ods.Doc
f, err := ods.NewReader(file, size)
if err != nil {
return nil, err
}
defer f.Close()
if err := f.ParseContent(&doc); err != nil {
return nil, err
}
for _, sheet := range doc.Table {
for _, row := range sheet.Strings() {
for _, text := range row {
if text != "" {
text = cleanCell(text)
cells = append(cells, text)
}
}
}
}
return
}