编辑 - 我重新添加了注释掉的部分,以包括我尝试解决此问题的其他方法,以希望能够更深入地了解我的过程。截至目前,该程序正在运行,但它会将我的本地电子表格作为附件发送,以代替我需要从谷歌表格生成并附加的“临时”文件。这显然是我遇到问题的部分,我需要知道如何相应地附加我的代码以下载并附加带有处理后的数据的临时文件...
您好,提前感谢您的帮助。当我尝试下载工作表文件并将其附加到电子邮件时,我的 Ruby 程序遇到了问题。该程序应该添加单位以从本地电子表格重新排序,并将其添加到工作表文件中,然后应下载该工作表并作为附件通过电子邮件发送。我可以让程序发送一封电子邮件,并在电子邮件正文中以文本形式包含相应的 SKU 和数量,并且我可以让程序将 SKU 和数量添加到指定的 google 工作表中,但我无法下载工作表文件并将其作为附件通过电子邮件发送。我收到的确切错误如下:
C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/signet-0.18.0/lib/signet/oauth_2/client.rb:420:in `scope=': Expected Array or String, got Hash (TypeError)
raise TypeError, "Expected Array or String, got #{new_scope.class}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/signet-0.18.0/lib/signet/oauth_2/client.rb:193:in `update!'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/signet-0.18.0/lib/signet/oauth_2/client.rb:115:in `initialize'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/googleauth-0.17.1/lib/googleauth/service_account.rb:105:in `initialize'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/googleauth-0.17.1/lib/googleauth/service_account.rb:80:in `new'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/googleauth-0.17.1/lib/googleauth/service_account.rb:80:in `make_creds'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/google_drive-3.0.7/lib/google_drive/session.rb:89:in `from_service_account_key'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/google_drive-3.0.7/lib/google_drive/session.rb:86:in `block in from_service_account_key'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/google_drive-3.0.7/lib/google_drive/session.rb:85:in `open'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/google_drive-3.0.7/lib/google_drive/session.rb:85:in `from_service_account_key'
from ex5.rb:38:in `insert_data'
from ex5.rb:83:in `process_order'
from ex5.rb:144:in `<main>'
有人可以检查我的代码并告诉我我做错了什么吗?
require 'bundler'
require 'google_drive'
require 'roo'
require 'google/apis/sheets_v4'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'mail'
Bundler.require
begin
def read_spreadsheet(file_path)
spreadsheet = Roo::Excelx.new(file_path)
header = spreadsheet.row(2)
data = []
(3..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
data << row
end
data
end
def format_data_plain_text(data)
formatted_data = ""
data.each do |row|
unless row['REORDER QTY'].to_i.zero? || row['AC SKU'].to_s.empty?
sku = row['AC SKU'].to_s
quantity = row['REORDER QTY'].to_s
formatted_data += "#{sku}\t#{quantity}\n"
end
end
formatted_data.to_s
end
def insert_data(file_path, formatted_data)
session = GoogleDrive::Session.from_service_account_key("client_secret.json")
spreadsheet_title = "Reorder Details"
spreadsheet = session.spreadsheet_by_title(spreadsheet_title)
worksheet = spreadsheet.worksheets.first
first_empty_row = 1
while !worksheet[first_empty_row, 1].empty? && first_empty_row <= worksheet.num_rows
first_empty_row += 1
end
if first_empty_row == 1
worksheet[first_empty_row, 1] = "SKU"
worksheet[first_empty_row, 2] = "QUANTITY"
first_empty_row += 1
end
puts formatted_data.class
data_lines = formatted_data.split("\n")
data_lines.each_with_index do |line, index|
columns = line.split("\t")
worksheet[first_empty_row + index, 1] = columns[0]
worksheet[first_empty_row + index, 2] = columns[1]
end
worksheet.save
end
def download_google_sheet(sheet_id)
session = GoogleDrive::Session.from_service_account_key("client_secret.json")
spreadsheet = session.spreadsheet_by_key(sheet_id)
worksheet = spreadsheet.worksheets.first
temp_file = Tempfile.new(['google_sheet', '.xlsx'], encoding: 'UTF-8')
worksheet.export_as_file(temp_file.path)
temp_file.path
temp_file.close
end
def process_order(file_path)
order_details = read_spreadsheet(file_path)
formatted_data = format_data_plain_text(order_details)
insert_data(file_path, formatted_data)
puts "Data inserted into Google Sheets."
sheet_id = '1Bx5wJgm2uw9H61yDb68hQlobSdXiS79U94Pe1alvor0'
temp_file_path = download_google_sheet(sheet_id)
send_email(file_path, temp_file_path)
end
def send_email(file_path, temp_file_path)
# Configure your email settings
options = {
address: 'smtp.gmail.com',
port: 587,
user_name: '[email protected]',
password: 'xouh zizs vwms mdtc',
domain: 'sparkmandistributing.com',
authentication: 'plain',
enable_starttls_auto: true
}
Mail.defaults do
delivery_method :smtp, options
end
order_details = read_spreadsheet(file_path)
formatted_data = format_data_plain_text(order_details)
#temp_file_content = File.read(temp_file_path)
#Specify the email content
mail = Mail.new do
from '[email protected]'
to '[email protected]'
subject "Order Details:"
body "Hey Tim. Hope you're doing well! I just need to place an order for the following items:\n\nThis will be using Net Terms.\n\nPlease let me know if you need anything else from me! Have a great week!"
add_file(file_path)
end
# Send the email
mail.deliver!
end
#def send_email(file_path)
# gmail = Gmail.connect("[email protected]", "0923istheKey!")
#
# subject = "Reorder Details"
# body = "Please find the attached Google Sheet with reorder details."
#
# gmail.deliver do
# to "[email protected]"
# subject subject
# text_part do
# body body
# end
# add_file file_path
# end
# gmail.logout
#end
file_path = 'C:\Users\zach\Documents\SPARKMAN DISTRIBUTING\AMAZON\COUNTS\012224 COUNT.xlsx'
process_order(file_path)
#def export_google_sheet(file_id, export_format, export_directory)
# session = GoogleDrive::Session.from_service_account_key('C:\Users\zach\Documents\CODING\RUBY\client_secret.json')
#
# spreadsheet = session.file_by_id(file_id)
# export_filename = "AC_Order_#{Time.now.strftime('%Y-%m-%d')}.#{export_format.split('/').last}"
# export_path = File.join(export_directory, export_filename)
#
# spreadsheet.export_as_file(export_format, export_path)
#
# puts "File exported to: #{export_path}"
#end
#file_id = '1Bx5wJgm2uw9H61yDb68hQlobSdXiS79U94Pe1alvor0'
#export_format = 'pdf'
#export_directory = 'C:\Users\zach\Documents\SPARKMAN DISTRIBUTING\AMAZON\ORDERS'
#export_google_sheet(file_id, export_format, export_directory)
end
我尝试在“format_data_plain_text”方法的末尾添加 format_data.to_s 以确保数据转换为字符串。我还尝试添加“temp_file_content = File.read(temp_file_path)”,但它一定是其他东西,因为无论这些添加如何,都会返回相同的错误。
Google::Session.from_service_account_key
具有以下方法签名:
def self.from_service_account_key(
json_key_path_or_io, scope = DEFAULT_SCOPE, client_options = nil,
request_options = nil
)
4 个位置参数是:
json_key_path_or_io
(必填)scope
(可选,默认为 DEFAULT_SCOPE
)client_options
(可选)request_options
(可选)如果你打电话
GoogleDrive::Session.from_service_account_key("client_secret.json", timeout_sec: 300)
然后
timeout_sec: 300
被视为 scope
参数的哈希,因此出现类型错误。
为了提供第三个参数,您还必须传递第二个参数:
GoogleDrive::Session.from_service_account_key(
"client_secret.json",
GoogleDrive::Session::DEFAULT_SCOPE,
timeout_sec: 300
)