I’m trying to write a simple function that appends a row to an existing Excel spreadsheet. The new row should be appended to the “bottom” of the data in the spreadsheet. I’m assuming that if there are existing rows in the sheet, there are no blank rows in between, so the function just needs to insert the new row into the first blank row.
Here’s what I have so far:
using XLSX
function append_xl_row(wb_path::String, sheet_name::String, row_data::Array)
XLSX.openxlsx(wb_path, mode = "w") do xf
sheet = xf[sheet_name]
sheet["A1"] = row_data #change this so it appends to the end
end
end
To test the function out, I created a new workbook and sheet:
wb_path = "C:/Users/Michael/Documents/test.xlsx"
sheet_name = "Sheet1"
row_data = [1, 1, 2, 3, 5, 8, 13, 21]
XLSX.openxlsx(wb_path, mode="w") do xf
XLSX.addsheet!(xf, sheet_name)
end
This currently writes the data to the first row of the Excel sheet.
My questions are:
- How do I find the first blank row and insert the new row there?
- Why is it that when I set
sheet_name
to be anything other than “Sheet1” (for example, “Sheet2” or “my_sheet”), I get the following error:
Sheet2 is not a valid sheetname or cell/range reference.
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] getdata(::XLSX.XLSXFile, ::String) at C:\Users\Michael\.julia\packages\XLSX\A7wWu\src\workbook.jl:130
[3] getindex(::XLSX.XLSXFile, ::String) at C:\Users\Michael\.julia\packages\XLSX\A7wWu\src\workbook.jl:93
[4] (::var"#103#104"{String,Array{Int64,1}})(::XLSX.XLSXFile) at .\In[125]:5
[5] openxlsx(::var"#103#104"{String,Array{Int64,1}}, ::String; mode::String, enable_cache::Bool) at C:\Users\Michael\.julia\packages\XLSX\A7wWu\src\read.jl:129
[6] append_xl_row(::String, ::String, ::Array{Int64,1}) at .\In[125]:4
[7] top-level scope at In[129]:1
[8] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
If I just do
wb_path = "C:/Users/Michael/Documents/test.xlsx"
sheet_name = "Sheet2"
row_data = [1, 1, 2, 3, 5, 8, 13, 21]
XLSX.openxlsx(wb_path, mode="w") do xf
XLSX.addsheet!(xf, sheet_name)
end
then I confirmed it does create a new sheet named “Sheet2”.:
1×1 XLSX.Worksheet: ["Sheet2"](A1:A1)
I checked and the new sheet appears in the Excel workbook. Yet I still get the “not a valid sheetname or cell/range reference” error when I execute append_xl_row
…Why does this happen?
Would greatly appreciate any help!