当我尝试使用拼音和win32ole使用以下代码设置较长的工作表名称时:
require "win32ole"
excel = WIN32OLE.new('Excel.Application')
excel.Visible = 1
puts excel.version
workbook = excel.Workbooks.Add
worksheet1 = workbook.Worksheets.Add
worksheet1.Name = "Pseudopseudohypoparathyroidism" #Length 30, fine
worksheet2 = workbook.Worksheets.Add
worksheet2.Name = "Supercalifragilisticexpialidocious" #Length 34, not fine
我得到了以下信息:
12.0
-:9:in `method_missing': (in setting property `Name': ) (WIN32OLERuntimeError)
OLE error code:800A03EC in Microsoft Office Excel
You typed an invalid name for a sheet or chart. Make sure that:
The name that you type does not exceed 31 characters.
The name does not contain any of the following characters: : \ / ? * [ or ]
You did not leave the name blank.
HRESULT error code:0x80020009
Exception occurred.
from -:9:in `<main>'
版本12.0表明我运行的是Excel2007,但它抱怨工作表名称太长。我看过this related answer中提到的Excel 2007 specifications and limits,但我找不到它提到任何这样的限制。(但是,尝试手动重命名工作表表明可能存在这样的限制)
是否有限制?是硬限制还是可以通过更改Excel的配置来更改?
发布于 2010-09-10 02:24:40
文件格式最多允许255个字符的工作表名称,但如果Excel UI不希望超过31个字符,请不要尝试超过31个字符。App充满了奇怪的无文档记录的限制和怪癖,向它提供符合规范但不在测试人员测试范围内的文件通常会导致非常奇怪的行为。(个人最喜欢的示例:在具有Excel 97样式的字符串表格的文件中,对if()函数使用Excel 4.0字节码时,禁用了Excel 97中的粗体工具栏按钮。)
发布于 2010-09-10 02:08:49
在Excel中手动重命名工作表时,您会遇到31个字符的限制,所以我建议这是一个硬限制。
发布于 2017-02-27 21:52:30
我使用下面的vba代码,其中filename是包含我想要的文件名的字符串,函数RemoveSpecialCharactersAndTruncate的定义如下:
worksheet1.Name = RemoveSpecialCharactersAndTruncate(filename)
'Function to remove special characters from file before saving
Private Function RemoveSpecialCharactersAndTruncate$(ByVal FormattedString$)
Dim IllegalCharacterSet$
Dim i As Integer
'Set of illegal characters
IllegalCharacterSet$ = "*." & Chr(34) & "//\[]:;|=,"
'Iterate through illegal characters and replace any instances
For i = 1 To Len(IllegalCharacterSet) - 1
FormattedString$ = Replace(FormattedString$, Mid(IllegalCharacterSet, i, 1), "")
Next
'Return the value capped at 31 characters (Excel limit)
RemoveSpecialCharactersAndTruncate$ = Left(FormattedString$, _
Application.WorksheetFunction.Min(Len(FormattedString), 31))
End Function
https://stackoverflow.com/questions/3681868
复制相似问题