Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查工作簿中是否存在工作表的Ruby脚本_Ruby_Windows_Excel_Win32ole - Fatal编程技术网

检查工作簿中是否存在工作表的Ruby脚本

检查工作簿中是否存在工作表的Ruby脚本,ruby,windows,excel,win32ole,Ruby,Windows,Excel,Win32ole,我需要知道工作簿中是否存在使用ruby的工作表 代码 excel = WIN32OLE.new('Excel.Application') excel.visible = false workbook = excel.Workbooks.Add(); worksheet = workbook.Worksheets.Add() workbook.Worksheets("header_new").copy(workbook.Worksheets("header_old")) 我需要将h

我需要知道工作簿中是否存在使用ruby的工作表

代码

excel = WIN32OLE.new('Excel.Application') 
excel.visible = false   
workbook = excel.Workbooks.Add(); 
worksheet = workbook.Worksheets.Add() 
workbook.Worksheets("header_new").copy(workbook.Worksheets("header_old")) 

我需要将
header\u old
的内容复制到
header\u new
中,除非后面的工作表存在,否则会抛出错误消息。

下面是一篇关于使用Ruby的好博文:

# Require the WIN32OLE library
require 'win32ole'
# Create an instance of the Excel application object
xl = WIN32OLE.new('Excel.Application')
# Make Excel visible
xl.visible = 1
# Add a new Workbook object
wb = xl.workbooks.add
# Get the first,second Worksheet
ws1,ws2 = wb.worksheets(1),wb.worksheets(2)
# Let rename those sheet
[ws1,ws2].each.with_index(1) { |s,i| s.name = "test_sheet_#{i}" }
# Lets check how many worksheet is present currently
totale_sheet_count = wb.sheets.count

# now let's check if any sheet is having the name, as you are looking for
1.upto(totale_sheet_count).any? { |number| wb.worksheets(number).name == "foo" } # => false
1.upto(totale_sheet_count).any? { |number| wb.worksheets(number).name == "test_sheet_2" } # => true
要理解这一点,您首先需要研究该方法,然后

以下是满足您需求的最终代码:

require 'win32ole'
excel = WIN32OLE.new( 'Excel.Application' )
excel.visible = true
wb = excel.workbooks.open( "path/to/your_excel.xlsx" )
totale_sheet_count = wb.sheets.count
# below line checking if your excel has any worksheet named as "header_new". If it
# find such a named sheet, Enumerable#any method will return true, otherwise false.
bol = 1.upto(totale_sheet_count).any? { |number| wb.worksheets(number).name == "header_new" }  

begin
  raise( RuntimeError, "Required sheet is not present" ) unless bol
  workbook.worksheets("header_new").copy(workbook.worksheets("header_old")) 
rescue RuntimeError => ex
  puts ex.message
end

你对鲁比生气吗?:-)顺便说一句,你现在在哪个操作系统?您是否使用?要获得有关stackoverflow的答案,您需要向我们展示您的尝试。它还有助于定义“工作表”等术语,让我们了解上下文。愤怒??不,我需要将某些excel数据更新到特定工作表中的工作簿中。如果工作表丢失,我需要抛出一条错误消息。我只使用windows。@user2818359将您的问题标记为
window
。首先给我们你所有的信息,然后只有我们能帮你。@Ziggy:对不起,我是Stackoverflow的新手…所以请不要告诉我规则。.最近我刚尝试了类似这样的东西:excel=WIN32OLE.new('excel.Application')excel.visible=false工作簿=excel.Workbooks.Add();worksheet=workbook.Worksheets.Add()工作簿.Worksheets(“header\u new”).copy(workbook.Worksheets(“header\u old”))现在我需要将header\u old的数据复制到header\u new buit中,然后我需要检查header\u new是否存在