Skip to content

Open existing workbook for update #10

Closed
jeantil opened this Issue Sep 3, 2012 · 7 comments

2 participants

@jeantil
jeantil commented Sep 3, 2012

Hi,

I would like to discuss how to add the possibility of opening an existing workbook template to edit, instead of creating a new one each time.

I can see that Workbook.scala would have to be changed:

  • add an apply method which takes a filename and "something" around line 78
  • change line11 to read from an inputstream when the filename exists

I am not sure what "something" should be :

  • A list of sheets each with a list of row each with a list of cells though.
    Then the wrapper would have to test if the sheet exists or not and use create or getSheet accordingly, same for row and cell

  • A list of transformations to be executed against the workbook ...

How would you go about it ?

@folone folone was assigned Sep 6, 2012
@folone
Owner
folone commented Sep 6, 2012

I will look into this. I'd add a way to merge Workbooks and a function to load a Workbook from file. The result would look something like this:

val path = "/path/to/file"

val existingBook = Workbook(path)

val newBook = Workbook {
  Sheet("name") {
    Row(1) {
      Cell(1, "data") :: Cell(2, "data2") :: Nil
    } ::
    Row(2) {
      Cell(1, "data") :: Cell(2, "data2") :: Nil
    } :: Nil
  } ::
  Sheet("name2") {
    Row(2) {
      Cell(1, "data") :: Cell(2, "data2") :: Nil
    } :: Nil
  } :: Nil
}

val resulting = existingBook |+| newBook // the second argument overwrites the first if needed.

resulting.safeToFile(path)

For this we need to provide an apply(path: String) for object Workbook and a merge function for it's class.

@folone folone pushed a commit that referenced this issue Sep 6, 2012
Georgii Leontiev re #10 Made Workbook, Sheet, Row and Cell a Semigroup. d8b6f74
@folone
Owner
folone commented Sep 6, 2012

Just added some code to enable Workbook merging. It is now possible to do the following:

$ ./sbt.sh
Detected sbt version 0.12.0
Starting sbt.sh: invoke with -help for other options
Using /home/folone/.sbt/0.12.0 as sbt dir, -sbt-dir to override.
[info] Loading project definition from /home/folone/workspace/poi.scala/project
[info] Set current project to poi (in build file:/home/folone/workspace/poi.scala/)
poi > console
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_07).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import info.folone.scala.poi._
import info.folone.scala.poi._

scala> import scalaz.syntax.semigroup._
import scalaz.syntax.semigroup._

scala> val sheetOne = Workbook {
     |   Sheet("name") {
     |     Row(1) {
     |       Cell(1, "data") :: Cell(2, "data2") :: Nil
     |     } ::
     |     Row(2) {
     |       Cell(1, "data") :: Cell(2, "data2") :: Nil
     |     } :: Nil
     |   } ::
     |   Sheet("name2") {
     |     Row(2) {
     |       Cell(1, "data") :: Cell(2, "data2") :: Nil
     |     } :: Nil
     |   } :: Nil
     | }
sheetOne: info.folone.scala.poi.Workbook = info.folone.scala.poi.Workbook@71c65b27

scala> val sheetTwo = Workbook {
     |   Sheet("name") {
     |     Row(1) {
     |       Cell(1, "newdata") :: Cell(2, "data2") :: Cell(3, "data3") ::Nil
     |     } ::
     |     Row(2) {
     |       Cell(1, "data") :: Cell(2, "data2") :: Nil
     |     } ::
     |     Row(3) {
     |       Cell(1, "data") :: Cell(2, "data2") :: Nil
     |     } :: Nil
     |   } :: Nil
     |   Sheet("name") {
     |     Row(2) {
     |       Cell(1, "data") :: Cell(2, "data2") :: Nil
     |     } :: Nil
     |   } :: Nil
     | }
sheetTwo: info.folone.scala.poi.Workbook = info.folone.scala.poi.Workbook@15eaa372

scala> sheetOne |+| sheetTwo
res0: info.folone.scala.poi.Workbook = info.folone.scala.poi.Workbook@4b1bdd73
@folone folone pushed a commit that referenced this issue Sep 6, 2012
Georgii Leontiev re #10 abstracted over merging, fixing travis build. accf8f9
@folone
Owner
folone commented Sep 6, 2012

Also, you can sum a list of Workbooks. I've updated readme.

@jeantil
jeantil commented Sep 8, 2012

Thanks this is exactly what I needed !

@folone
Owner
folone commented Sep 10, 2012

@jeantil I've implemented the loading from file (details in readme). Please report any bugs found.

@folone folone closed this Sep 10, 2012
@jeantil
jeantil commented Sep 10, 2012

I will report anything I find. I have to convert my quick and dirty hack to using poi.scala first though, as this is for a hobby project I have to prioritize aggressively and I won't be able to do it right away :( (possibly next weekend)

@folone
Owner
folone commented Sep 10, 2012

Great. Please note, that it now uses Sets instead of Lists. And it also won't preserve any cell styling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.