Skip to content

Instantly share code, notes, and snippets.

@iamdionysus
Last active August 29, 2015 14:18
Show Gist options
  • Save iamdionysus/9857fd6a25ec5ae6ddbb to your computer and use it in GitHub Desktop.
Save iamdionysus/9857fd6a25ec5ae6ddbb to your computer and use it in GitHub Desktop.
ruby win32ole copy and paste last row in excel
excel = WIN32OLE.new 'Excel.Application'
excel.visible = true
workbook = excel.Workbooks.open "path/to/file"
sheet = workbook.Worksheets 1
def excel_copy_and_paste_last_row excel, workbook, sheet
last_row = sheet.UsedRange.Rows.Count
sheet.Rows(last_row).copy
sheet.Cells(last_row + 1, 1).Select
workbook.ActiveSheet.Paste
excel.CutCopyMode = false
end
def excel_save_and_close excel, workbook
# save xlsx
workbook.Saved = true
workbook.Save
# close excel
excel.activeworkbook.close 0
excel.quit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment