在Excel中,是否可以在连接另一列时自动将一列转换为上标?

huangapple go评论108阅读模式
英文:

Is it possible to automatically convert one column to superscript while concatenating with another column in Excel?

问题

我试图在Excel中连接两列,以便其中一列以上标形式显示,如下所示:

在Excel中,是否可以在连接另一列时自动将一列转换为上标?

在Excel中是否有可能当我在B列和C列中添加数据时,结果显示在D列中,如附图所示,以便我不必手动更改D列中的每一行的第一部分为上标?

我尝试将B列的格式更改为上标,然后使用连接公式,但它会在结果列D中更改为正常格式。

任何帮助将不胜感激!

英文:

I am trying to Concat 2 columns in Excel in such a way that one column is shown in superscripted form as shown below

在Excel中,是否可以在连接另一列时自动将一列转换为上标?

Is it possible in Excel that when I add data in columns B and C, the result is shown in column D as shown in the snapshot attached so that I don't have to manually change the first part in column D in every row to superscript?

I tried changing the formatting of column B to superscript and then use concat formula but it changes it to normal formatting in the resultant column D.

Any help will be highly appreciated!

答案1

得分: 1

(Note: 由于简化起见,此答案假设您使用支持switchifs的Excel版本,而这在Excel 2010中不适用。但是,将其转换为标准的if应该相对简单。如果您没有unichar函数,解决方案会变得更加复杂。)

让我们从最后开始,逆向思考。假设您在Excel中有一张表,将一个数字n映射到其上标等效项。类似于这样的表格:

n 上标
0
1 ¹
2 ²
3 ³
4
5
6
7
8
9
10 ¹⁰

然后,您可以执行如下简单的VLOOKUP来将上标数字与文本连接起来:=VLOOKUP([@Rank],tSuperscripts,2,FALSE)&" "&[@Text],这将给您这样的结果:¹ Some String。(出于简化的考虑,我没有添加括号;然而,如下所示,这将很容易实现。)

因此,问题变成了如何填充您的“查找表”。从0到9派生数字的上标很容易:处理n在(1,2,3)的特殊情况,然后您只需参考维基百科编写一个使用unichar的函数,类似于这样:

=UNICHAR(SWITCH([@n],1,HEX2DEC("b9"),2,HEX2DEC("b2"),3,HEX2DEC("b3"),HEX2DEC("2070")+[@n]))

对于大于等于10的数字,情况就变得更加复杂。在那一点上,您最好的选择是拥有“辅助”列,将原始数字拆分为组成数字,派生它们的上标,然后将所有内容连接起来。为此,您可以做类似于以下的操作(基本上有列来提取特定的数字,然后有另一列来派生该数字的上标,然后将所有内容连接成一个列)。请注意使用了--。由于LEFTMID将数字转换为字符串:

n 上标 n1 n2 n3 n1的上标 n2的上标 n3的上标
0 =CONCAT([@[n1的上标]],[@[n2的上标]],[@[n3的上标]]) =--LEFT([@n],1) =IF([@n]>=10,--MID([@n],2,1),"") =IF([@n]>=100,--MID([@n],3,1),"") =UNICHAR(SWITCH([@n1],1,HEX2DEC("b9"),2,HEX2DEC("b2"),3,HEX2DEC("b3"),HEX2DEC("2070")+[@n1])) =IF([@n]>=10,UNICHAR(SWITCH([@n2],1,HEX2DEC("b9"),2,HEX2DEC("b2"),3,HEX2DEC("b3"),HEX2DEC("2070")+[@n2]),"") =IF([@n]>=100,UNICHAR(SWITCH([@n3],1,HEX2DEC("b9"),2,HEX2DEC("b2"),3,HEX2DEC("b3"),HEX2DEC("2070")+[@n3]),"")

请注意,您可以相对容易地扩展此逻辑以处理大于等于100、1,000等的数字。

一旦您填充了此表,就可以使用顶部显示的vlookup。如上所述,如果需要括号(0x207d和0x207e),您可以将它们添加到查找表中。例如:=CONCAT(UNICHAR(HEX2DEC("207d")),[@[n1的上标]],[@[n2的上标]],[@[n3的上标]],UNICHAR(HEX2DEC("207e")))

英文:

(Note: For the sake of simplicity, this answer is written assuming you have a version of Excel that supports switch and ifs, which wouldn't be the case in Excel 2010. However, it would be pretty straightforward to convert to standard ifs.). If you don't have the unichar function, the solution would get much more tricky.

Let's start at the end and work our way backwards. Let's say you had a table in Excel that mapped a number n to its superscript equivalent. Something like this:

n superscript
0
1 ¹
2 ²
3 ³
4
5
6
7
8
9
10 ¹⁰

Then you could do as something as easy as doing a VLOOKUP to concatenate the superscripted number with the text: =VLOOKUP([@Rank],tSuperscripts,2,FALSE)&" "&[@Text], which would give you this: ¹ Some String. (For the sake of simplicity, I didn't add in the parentheses; however, that would be trivial to do, as you'll see below).

So then the question becomes how to populate your "lookup table". Deriving the superscript for a number from 0 to 9 is easy enough: Handle the special cases of n in (1,2,3), and you can just reference Wikipedia to write a function using unichar. Something like this:

=UNICHAR(SWITCH([@n],1,HEX2DEC("b9"),2,HEX2DEC("b2"),3,HEX2DEC("b3"),HEX2DEC("2070")+[@n]))

For numbers ≥ 10, it gets a bet more complicated. I think your best bet at that point would be to have "helper" columns that split out the original number into constituent digits, derive their superscripts, and then concatenate everything back. To that end, you could do something like this (basically have columns to pull a specific digit, and then have another column to derive the superscript for that digit, then concatenate everything into 1 column). Note the use of --. Since LEFT and MID turn the digit into a string

n superscript n1 n2 n1_super n2_super
0 =CONCAT([@[n1_super]],[@[n2_super]],[@[n3_super]]) =--LEFT([@n],1) =IF([@n]>=10,--MID([@n],2,1),"") =UNICHAR(SWITCH([@n1],1,HEX2DEC("b9"),2,HEX2DEC("b2"),3,HEX2DEC("b3"),HEX2DEC("2070")+[@n1])) =IF([@n]>=10,UNICHAR(SWITCH([@n2],1,HEX2DEC("b9"),2,HEX2DEC("b2"),3,HEX2DEC("b3"),HEX2DEC("2070")+[@n2])),"")

Note that you can extend this logic pretty easily for numbers ≥ 100, 1,000, etc.

Once you have this table populated, you can use the vlookup shown at the top. As stated, if you need the parentheses (0x207d and 0x207e), you can add those in to the lookup table. For example: =CONCAT(UNICHAR(HEX2DEC("207d")),[@[n1_super]],[@[n2_super]],[@[n3_super]],UNICHAR(HEX2DEC("207e")))

答案2

得分: 1

You can use UNICHAR function to accomplish this. The user LAMBDA function SUPERSCRPT(x), does the conversion of the input string (x) considering each case (formula 1):

=LET(SUPERSCRPT, LAMBDA(x, LET(lk, HSTACK(
  VSTACK("&" & SEQUENCE(10,,0),{"(";")"}),
  VSTACK({8304;185;178;179},SEQUENCE(6,,8308),{8317;8318})),
 y,MID(x,SEQUENCE(LEN(x)),1),
 REDUCE("",y,LAMBDA(ac,a,ac & UNICHAR(XLOOKUP(a,INDEX(lk,,1),INDEX(lk,,2))))))),
 BYROW(A2:B6, LAMBDA(x,SUPERSCRPT(INDEX(x,1))&INDEX(x,2))))

You can also define SUPERSCRPT as an array function, so you don't have to build the lookup table (lk) on every call and you can use it for a single cell or a single column array (formula 2):

=LET(SUPERSCRPT,LAMBDA(x, LET(lk, HSTACK(
  VSTACK("&" & SEQUENCE(10,,0),{"(";")"}),
  VSTACK({8304;185;178;179},SEQUENCE(6,,8308),{8317;8318})),
  BYROW(x, LAMBDA(xi,REDUCE("",MID(xi,SEQUENCE(LEN(xi)),1),
   LAMBDA(ac,a, ac & UNICHAR(XLOOKUP(a,INDEX(lk,,1),INDEX(lk,,2))))))))),
  SUPERSCRPT(A2:A6)&B2:B6)

Here is the output:

在Excel中,是否可以在连接另一列时自动将一列转换为上标?

What is does is first to build a lookup table (lk) you can have it outside of the formula in the Excel spreadsheet and reference it. This table has two columns: input (text data type), output (UNICODE decimal number). Here is the reference table:

Input Code
"0" 8304
"1" 185
"2" 178
"3" 179
"4" 8308
"5" 8309
"6" 8310
"7" 8311
"8" 8312
"9" 8313
"(" 8317
")" 8318

For formula 1: The input (x) is split by rows (y) via MID function. We use REDUCE to do the recursion for each element (a) of y. On each iteration it finds the input value (a) in the lookup table (lk) via XLOOKUP and extracts the code number, then invokes UNICHAR function and concatenate the result to the accumulator (ac). We use BYROW to spill the array result for the input range.

I found the corresponding UNICODE values here: Unicode subscripts and superscripts, section: Superscripts and subscripts block. Numbers are represented in hexadecimal. For example the superscript 1 (¹) is represented by the hexadecimal number: 00B9. The lk table in the formula uses the equivalent decimal number, so basically I converted them via HEX2DEC Excel function. For example: =HEX2DEC("00B9") which returns 185.

英文:

You can use UNICHAR function to accomplish this. The user LAMBDA function SUPERSCRPT(x), does the conversion of the input string (x) considering each case (formula 1):

=LET(SUPERSCRPT, LAMBDA(x, LET(lk, HSTACK(
  VSTACK(""&SEQUENCE(10,,0),{"(";")"}),
  VSTACK({8304;185;178;179},SEQUENCE(6,,8308),{8317;8318})),
 y,MID(x,SEQUENCE(LEN(x)),1),
 REDUCE("",y,LAMBDA(ac,a,ac & UNICHAR(XLOOKUP(a,INDEX(lk,,1),INDEX(lk,,2))))))),
 BYROW(A2:B6, LAMBDA(x,SUPERSCRPT(INDEX(x,1))&INDEX(x,2))))

You can also define SUPERSCRPT as an array function, so you don't have to build the lookup table (lk) on every call and you can use it for a single cell or a single column array (formula 2):

=LET(SUPERSCRPT,LAMBDA(x, LET(lk, HSTACK(
  VSTACK(""&SEQUENCE(10,,0),{"(";")"}),
  VSTACK({8304;185;178;179},SEQUENCE(6,,8308),{8317;8318})),
  BYROW(x, LAMBDA(xi,REDUCE("",MID(xi,SEQUENCE(LEN(xi)),1),
   LAMBDA(ac,a, ac & UNICHAR(XLOOKUP(a,INDEX(lk,,1),INDEX(lk,,2))))))))),
  SUPERSCRPT(A2:A6)&B2:B6)

Here is the output:
在Excel中,是否可以在连接另一列时自动将一列转换为上标?

What is does is first to build a lookup table (lk) you can have it outside of the formula in the Excel spreadsheet and reference it. This table has two columns: input (text data type), output (UNICODE decimal number). Here is the reference table:

Input Code
"0" 8304
"1" 185
"2" 178
"3" 179
"4" 8308
"5" 8309
"6" 8310
"7" 8311
"8" 8312
"9" 8313
"(" 8317
")" 8318

For formula 1: The input (x) is split by rows (y) via MID function. We use REDUCE to do the recursion for each element (a) of y. On each iteration it finds the input value (a) in the lookup table (lk) via XLOOKUP and extracts the code number, then invokes UNICHAR function and concatenate the result to the accumulator (ac). We use BYROW to spill the array result for the input range.

I found the corresponding UNICODE values here: Unicode subscripts and superscripts, section: Superscripts and subscripts block. Numbers are represented in hexadecimal. For example the superscript 1 (¹) is represented by the hexadecimal number: 00B9. The lk table in the formula uses the equivalent decimal number, so basically I converted them via HEX2DEC Excel function. For example: =HEX2DEC("00B9") which returns 185.

huangapple
  • 本文由 发表于 2023年5月24日 18:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定