Zach S
Zach S

Reputation: 15

ENOENT Error when trying to create Excel file in Ruby

EDIT 2: I've managed to resolve the directory issue and it creates the CSV file without issue, however when I try to export the file as a XLSX file using the save_to_xlsx method, the program still saves the file as a CSV even though I've explicitly changed the file_format within the save_to_xlsx method. I noticed if I change file_format at the top of my code, it sticks with the initial format and doesn't seem to change.

I want my program to allow the user to choose whether they want to export the processed data as a CSV or Excel sheet and then save the file in that format while also including today's date in the file name, but when I run the program and it asks which format to save it under, the console returns a "No Such File or Directory" error. The strange thing is I swear this code was working fine in the past but today it's throwing this error for what seems like multiple directories.

Here is the portion of code where I'm running into the error:

require 'bundler'
require 'roo'
require 'mail'
require 'date'
require 'csv'
require 'google_drive'
require 'googleauth'
Bundler.require

file_format = 'xlsx'
sheet_id = 'abc123'
local_file_path = "C:\\Users\\zach\\Documents\\Business\\ORDERS\\output.#{file_format}"
csv_file_path = "C:\\Users\\zach\\Documents\\Business\\ORDERS\\output.csv"
file_path = 'C:\Users\zach\Documents\Business\COUNTS\012224 COUNT.xlsx'


def save_to_csv(local_file_path, formatted_data, header)

  CSV.open(local_file_path, 'w') do |csv|
    csv << header
    formatted_data.each_line do |line|
    csv << line.chomp.split("\t")
    end
  end
end

def save_to_xlsx(sheet_id, local_file_path)
  session = GoogleDrive::Session.from_service_account_key("client_secret.json")
  spreadsheet = session.spreadsheet_by_key(sheet_id)

  worksheet = spreadsheet.worksheets.first

  spreadsheet.export_as_file(local_file_path)
  puts "XLSX file has been saved to the following location:\n#{local_file_path}.\n\n"
end

def process_order(sheet_id, file_path, file_format)
  today_date = Date.today.strftime("%m.%d.%Y")
  local_file_path = "C:\\Users\\zach\\Documents\\Business\\ORDERS\\AC_ORDER_#{today_date}.#{file_format}"
  order_details = read_spreadsheet(file_path)
  formatted_data = format_data_plain_text(order_details)
  user_input = 0
  puts "Save as type:\n\n1.) CSV\n\n2.) XLSX"
  user_input = gets.chomp.to_i
  if user_input == 1
    file_format = 'csv'
    save_to_csv(local_file_path, formatted_data, ['AC SKU', 'REORDER QTY'])
  elsif user_input == 2
    file_format = 'xlsx'
    save_to_xlsx(sheet_id, local_file_path)
  else
    puts "invalid selection. Please choose from the available options.\n\n"
    return
  end
  send_email(file_path, local_file_path)
end

Any ideas as to why this would have been able to execute and save in the past but not now? I have a file from a previous date in the same output directory that was generated using the same program so I'm just really confused as to what I may have changed or where I'm going wrong. TIA for any support.

EDIT Here is the stack trace for reference:

C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/csv-3.2.8/lib/csv.rb:1594:in `initialize': No such file or directory @ rb_sysopen - C:\Users\zach\Documents\Business\ORDERS\AC_ORDER_03.12.2024.xlsx (Errno::ENOENT)
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/csv-3.2.8/lib/csv.rb:1594:in `open'
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/csv-3.2.8/lib/csv.rb:1594:in `open'
        from ex6.rb:44:in `save_to_csv'
        from ex6.rb:72:in `process_order'
        from ex6.rb:118:in `<main>'

Upvotes: 0

Views: 70

Answers (1)

Zach S
Zach S

Reputation: 15

I managed to get the program working successfully by re-copying the path to the directory, replacing the path that was initially in local_file_path and also adding the local_file_path variable before calling save_to_xlsx and save_to_csv in the process_order method. I think part of the issue was the methods were using the local_file_path argument but not changing the file_format variable outside of the method, so as a result the file being exported would end in whatever file_format was initialized to at the top of the code.

This is how the portion of code looks now:

def save_to_csv(local_file_path, formatted_data, header)

  CSV.open(local_file_path, 'w') do |csv|
    csv << header
    formatted_data.each_line do |line|
    csv << line.chomp.split("\t")
    end

    puts "CSV file has been saved to the following location:\n#{local_file_path}.\n\n"
  end
end

def save_to_xlsx(sheet_id, local_file_path)
  session = GoogleDrive::Session.from_service_account_key("client_secret.json")
  spreadsheet = session.spreadsheet_by_key(sheet_id)

  worksheet = spreadsheet.worksheets.first

  spreadsheet.export_as_file(local_file_path)
  puts "XLSX file has been saved to the following location:\n#{local_file_path}.\n\n"
end

def process_order(sheet_id, file_path, file_format, local_file_pat, today_date)
  order_details = read_spreadsheet(file_path)
  formatted_data = format_data_plain_text(order_details)
  user_input = 0
  puts "Save as type:\n\n1.) CSV\n\n2.) XLSX"
  user_input = gets.chomp.to_i
  if user_input == 1
    file_format = 'csv'
    local_file_path = "C:\\Users\\zach\\Documents\\Business\\ORDERS\\AC_ORDER_#{today_date}.#{file_format}"
    save_to_csv(local_file_path, formatted_data, ['AC SKU', 'REORDER QTY'])
  elsif user_input == 2
    file_format = 'xlsx'
    local_file_path = "C:\\Users\\zach\\Documents\\Business\\ORDERS\\AC_ORDER_#{today_date}.#{file_format}"
    save_to_xlsx(sheet_id, local_file_path)
  else
    puts "invalid selection. Please choose from the available options.\n\n"
    return
  end

Upvotes: 0

Related Questions