Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Support updating an existing spreadsheet #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions src/main/groovy/org/gsheets/ExcelFile.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ package org.gsheets

import org.apache.poi.hssf.usermodel.HSSFRichTextString
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.util.CellRangeAddress
import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.ss.util.CellRangeAddress

/**
* A Groovy builder that wraps Apache POI for generating binary Microsoft Excel sheets.
Expand Down Expand Up @@ -78,13 +77,49 @@ import org.apache.poi.hssf.usermodel.HSSFCell
*/
class ExcelFile {

Workbook workbook = new HSSFWorkbook()
Workbook workbook
private Sheet sheet
private int rowsCounter

private Map<String, CellStyle> cellStyles = [:]
private Map<String, Font> fonts = [:]


/**
* Creates a builder for a new file
*/
def ExcelFile() {
workbook = new HSSFWorkbook()
}

/**
* Creates a builder based on an existing file.
*
* All modifications will be on top of the given file, which
* serves as a base template.
*
* @param filename Path to the file
*/
def ExcelFile(String filename) {
this(new File(filename))
}

/**
* Creates a builder based on an existing file.
*
* All modifications will be on top of the given file, which
* serves as a base template.
*
* @param file Existing file
*/
def ExcelFile(File file) {
assert file.exists()

def input = new FileInputStream(file)
workbook = new HSSFWorkbook(input)
input.close()
}

/**
* Creates a new workbook.
*
Expand Down Expand Up @@ -126,7 +161,7 @@ class ExcelFile {
assert name
assert closure

sheet = workbook.createSheet(name)
sheet = workbook.getSheet(name) ?: workbook.createSheet(name)
rowsCounter = 0

closure.delegate = sheet
Expand Down Expand Up @@ -273,6 +308,15 @@ class ExcelFile {
sheet.createRow(rowsCounter++ as int)
}

void onRow (int r, Closure closure) {
assert sheet
assert r >= 0

rowsCounter = r
closure.delegate = this
closure.call()
}

void row(values) {
assert sheet
assert values
Expand Down
57 changes: 55 additions & 2 deletions src/test/groovy/org/gsheets/ExcelFileTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
*/
package org.gsheets

import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.CellStyle
import org.apache.poi.ss.usermodel.Font
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.ss.usermodel.Workbook

/**
* @author [email protected]
Expand Down Expand Up @@ -83,4 +82,58 @@ class ExcelFileTests extends GroovyTestCase {
workbook.write(excelOut)
excelOut.close()
}

void testOpenWorkbookAndEditExistingSheet() {
def file = createTestFile()
def workbook = new ExcelFile(file).workbook {
data {
sheet("SheetA") {
onRow(4) {
row (['new row', 'on existing', 'workbook'])
}
}
}
}

def out = new FileOutputStream(file)
workbook.write(out)
out.close()
}

void testOpenWorkbookAndAddNewSheet() {
def file = createTestFile()
def workbook = new ExcelFile(file).workbook {
data {
sheet("SheetB") {
header (["New", "Header"])
row (["new", "row"])
}
}
}

def out = new FileOutputStream(file)
workbook.write(out)
out.close()
}

private File createTestFile() {
def file = File.createTempFile("gsheets", ".xls")
def workbook = new ExcelFile().workbook {
data {
sheet ("SheetA") {
header (['HA', 'HB', 'HC'])

3.times {
row ([it, 2*it, 3*it])
}
}
}
}

def out = new FileOutputStream(file)
workbook.write(out)
out.close()

file
}
}