使用 Office 脚本在线读取 Excel 单元格中的 superScripts 字符
我正在尝试使用 Office 脚本读取 Excel 中的内容,该 Excel 的单元格包含多个数字,其中一些数字的格式为上标。当我尝试使用 Office 脚本(Typescript)从这些单元格中读取数据时,它会将单元格的内容返回为普通数字(完全忽略上标,将它们显示为通常的数字)。 另一个问题是,一旦我在线选择 Excel 中的单元格内容,该单元格就会丢失其上标格式,而我只能使用(CTRL + Z)将其恢复。
我尝试在同一个 Excel 文件中填写一个新单元格,但这次使用包含我创建并从该网站复制的上标的数据 SuperScriptGenerator:现在我可以选择单元格的内容而不会丢失上标字符的格式,我还可以使用office脚本读取单元格的内容,这将在保留单元格内容的同时显示单元格的内容上标字符。
我知道这可能是文本编码问题,但在这种情况下,在 excel online 上,我无法识别文本的编码?也不知道为什么我从上标生成器复制并粘贴到 Excel 上的字符串工作正常,而已经存在的上标却导致了问题(一旦我选择单元格内容,上标格式就会消失,并且在使用 OfficeScripts/ 读取单元格内容时显示为通常的数字)打字稿)!关于此案的任何帮助或提示。谢谢
I am trying to read content from excel using office scripts , cells of this excel contain multiple numbers , some of those are formatted as superscripts. when I try to read data from these cells with office scripts ( Typescript) it returns the content of cell as normal numbers ( completely ignoring the superscripts , displaying them as usual numbers ).
Another issue , is that once I select the content of the cell in excel online , the cell loses its superscript formatting ,and I get it back only using ( CTRL + Z) .
I tried to fill in a new cell in the same excel file , but this time with data containing superscripts that I created and copied from this site SuperScriptGenerator : Now I can select the content of the cell without losing the formatting of the superscript characters , I can also read the content of the cell with office scripts , and that would display the content of the cell while preserving the superscript characters.
Am aware that this could be a text encoding issue , but in this case , on excel online , I couldn't identify the encoding of the text ? neither why the string I copied from the superscript generator and pasted on the excel worked fine , while the already existing superscripts are causing issues ( superscripts formatting disappears once I select the cell content, and appearing as usual numbers when reading the cell content with officeScripts/typescript) ! Any help or hints on this case. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的理解是,默认情况下,Office 应用程序(Excel、Word、PowerPoint 等)中的上标/下标文本实际上只是使用特殊格式属性呈现的常规字符。我认为一个很好的类比可能是
< /a> 和
HTML 中的标签。
例如,在 HTML 中,
A2 + B2 = C2
将呈现为:但是,如果您将其复制并粘贴到记事本等纯文本编辑器中,您会发现它会变成:
这是因为纯文本的任何格式设置属性(在本例中为
属性)都会丢失。
Office 脚本 API
getValue
和getText
具有相同的行为,因为目前它们仅支持纯文本/数字,没有任何格式属性。但你会问 - 为什么从 SuperScriptGenerator 复制的文本可以保留其上标格式?
答案是“Unicode符号”!
现在您可以尝试复制此文本并再次粘贴到记事本中:
您会发现上标这次在记事本中看起来仍然正确!
这是因为这里不涉及特殊的上标格式属性。小数字
²
只是一个特殊的Unicode符号,可以与任何支持Unicode的纯文本一起保留。显然 SuperScriptGenerator 使用这些 Unicode 符号来表示上标,这样在翻译中就不会丢失。
顺便说一句,Excel Online 中的选择行为对我来说确实听起来像是一个错误(或者至少是一个限制)。似乎它会使用第一个字符的格式属性自动格式化单元格中的所有字符。因此,如果单元格中有类似“2A”的内容,您在编辑模式下选择它并移出,最终会得到“2A” ”。
My understanding is that by default superscript/subscript texts in Office Apps (Excel, Word, PowerPoint, etc.) are actually just regular characters rendered with special formatting properties. I think a good analog of this is probably the
<sup>
and<sub>
tags in HTML.For example, in HTML,
A<sup>2</sup> + B<sup>2</sup> = C<sup>2</sup>
will be rendered as:But if you copy it and paste into a plain text editor like Notepad, you will notice it will become:
This is because any formatting properties (in this case, the
<sup>
property) would get lost for plain text.The Office Scripts APIs
getValue
andgetText
has the same behavior since currently they only support plain texts/numbers without any formatting properties.But then you would ask - why can the text copied from SuperScriptGenerator retain their superscript formatting?
The answer is "Unicode Symbols"!
Now you can try to copy this text and paste into Notepad again:
You'll notice the superscripts still look correct in Notepad this time!
This is because there are no special superscript formatting properties involved here. The small number
²
is just a special Unicode Symbol that can be retained with any plain text that supports Unicode.So apparently SuperScriptGenerator uses those Unicode Symbols to represent superscripts, which won't get lost in translation.
BTW, the selecting behavior in Excel Online does sound to me like a bug (or at least a limitation). It seems like it will automatically format all the characters in the cell with the formatting properties of the first character. So if you have something like "2A" in a cell, you select it in edit mode and move out you will end up with "2A".