renebigot / XlsxReaderWriter

XlsxReaderWriter is an Objective-C library for iPhone / iPad and Mac. It parses and writes MS Excel XLSX files.
MIT License
438 stars 121 forks source link

Swift does not commit changes to columns? #46

Closed trailburner closed 8 years ago

trailburner commented 8 years ago

So I'm using swift then I send the file through an email code but the changes are not commited I still see the excel file with no records inserted Am I doing something wrong or did I miss something?

Thanks

    let documentPath: String = NSBundle.mainBundle().pathForResource("backuptemplate", ofType: "xlsx")!
    let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(documentPath)
    let worksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet

    let rownum = 2
    let cellheads = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    let contents: [String] = ["20","0","0","2016/05/29","This is Just a Dummy Data","8","22","1","1","0","0","0","0","0","0","0","0"]
    for i in 0...16{
        print("inserting to \(cellheads[cellheads.startIndex.advancedBy(i)])\(rownum)")
        let celltoinsert = "\(cellheads[cellheads.startIndex.advancedBy(i)])\(rownum)"
        //worksheet.cellForCellReference("\(celltoinsert)", shouldCreate: true).stringValue = contents[i]
        worksheet.cellForCellReference(celltoinsert, shouldCreate: true).setStringValue(contents[i])
    }

    spreadsheet.save()
renebigot commented 8 years ago

Haven't tested it yet, but it seems you are trying to save a file present in the main bundle. Save your file in a cache folder from the sandbox, then send the saved copy by email, it should work.

AmbroseSilveira commented 7 years ago

@trailburner Did you manage to solve this issue? could you send the updated file?

trailburner commented 7 years ago

@AmbroseSilveira nope I did not get time to look into it.... let me know if you can solve it maybe we can exchange notes

AmbroseSilveira commented 7 years ago

@trailburner I was working on similar kind of issue where i had to write in an excel file. My observation was,

  1. it is possible to write in an excel file if there are no formulas in the cell.
    1. If formulas do exist for a cell and if you try to write, you cant commit it to the excel file unless externally/manually the excel file is opened and saved.

The good news is i have solution for your problem: problem: you are reading a file from bundle resource of the path which has only read access. solution:So, you gotta make a copy of the excel file in document directory and then write onto it , preform the save operation and and send the file.
here a chunk of code that might help you:

let doc = NSSearchpathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let path = doc.appending("/filename.xlsx") let filemanager = Filemanager.default if !filemanager.fileExists(atPath:path) { let resourcePath = Bundle.main.path(forResource:"filename", ofType:"xlsx") do { try filemanager.copyItem(atPath:resourcePath!, toPath:path) } catch { print("error") } }

//now perform reading from document directory xlsx file and perform write and save operation and see. It should work unless formulas are not in picture.