79338595

Date: 2025-01-08 09:44:32
Score: 0.5
Natty:
Report link

I found a solution, which works alright. It works with the win32com client though, so I think, that it only works on Windows. But maybe it helps someone. It adds "number_of_rows"-rows after the "start_row"-row:

insert_empty_rows <- function(filename, sheet_name, start_row, number_of_rows){
  
  
  # create an instance of Excel
  excel_app <- RDCOMClient::COMCreate("Excel.Application")
  
  # hide excel
  excel_app[["Visible"]] <- FALSE
  excel_app[["DisplayAlerts"]] <- FALSE
  
  
  # open workbook
  wb_rdcom <- excel_app$Workbooks()$Open(filename)
  ws_rdcom <- wb_rdcom$Sheets(sheet_name)

  # insert lines
  for (. in 1:number_of_rows){
    ws_rdcom$Rows(start_row + 1)$Insert()
  }
  
  
  # save and close workbook
  wb_rdcom$Save()
  wb_rdcom$Close()
  excel_app$Quit()
  
  # clean up
  rm(excel_app, wb_rdcom)
  wb_rdcom <- NULL
  excel_app <- NULL
  gc()
  
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: djang0