简体   繁体   English

使用 excel 中的默认标头转置

[英]Transpose with default headers in excel

I have a simple data which is我有一个简单的数据是

Name   Age
Venky   20
Anil    22

Output should be like: Output 应该像:

Name : Venky
Age  : 20
Name : Anil
Age  : 22

Note: For each record should have header values注意:对于每条记录都应该有 header 值

I have tried multiple ways apart from Macros Can you please give me you inputs?除了宏,我尝试了多种方法,你能给我输入吗?

Without more specific context we can play with BYROW HSTACK , REDUCE , TOCOL , ARRAYTOTEXT , TEXTSPLIT and SUBSTITUTE to get the expected result:如果没有更具体的上下文,我们可以使用BYROW HSTACKREDUCETOCOLARRAYTOTEXTTEXTSPLITSUBSTITUTE来获得预期的结果:

=LET(z,IF(ISNUMBER(SEQUENCE(ROWS(TOCOL(A1:B1)),1,1)),";",),y,
    SUBSTITUTE(ARRAYTOTEXT(BYROW(A2:B3, LAMBDA(x, SUBSTITUTE(SUBSTITUTE(
    REDUCE("", HSTACK(TOCOL(A1:B1), TOCOL(x), z), 
    LAMBDA(a,b, IF(a="", b, a & ":" & b))), 
    ":;:", ";"),":;", ";")))),";, ",";") , 
    TEXTSPLIT(LEFT(y, LEN(y) - 1),,";"))

The z variable is just a trick for generating a column array of constant values ( ; ) with the same size as the header, that we are going to use as row delimiter, maybe there is an easier way to do it, but this works. z变量只是生成与 header 大小相同的常量值 ( ; ) 的列数组的一个技巧,我们将使用它作为行分隔符,也许有更简单的方法来做到这一点,但这有效。

IF(ISNUMBER(SEQUENCE(ROWS(TOCOL(A1:B1)),1,1)),";",)

The main idea is create on the fly for each row of input data( A1:B3 including the headers) a new array via HSTACK with the following structure (let's call it tempArray :主要思想是通过HSTACK每一行输入数据(包括标题)动态创建一个新数组(我们tempArray A1:B3

|Name | name1 |; |
------------------
|Age  | age1  |; |
------------------

Note : It should work for more than two columns in the header注意:它应该适用于 header 中的两列以上

Then reduce tempArray with REDUCE to concatenate each value with : .然后使用REDUCE减少tempArray以将每个值与:连接起来。 At this point each row x has been trasformed in to a string.此时,每一行x都已转换为一个字符串。

Clean the delimiter generated with SUBSTITUTE to remove unnecessary repeated delimiters.清理使用SUBSTITUTE生成的分隔符以删除不必要的重复分隔符。 Then convert the entire Array generated by BYROW with ARRAYTOTEXT to a single string that has each row delimited by ;然后将BYROW使用ARRAYTOTEXT生成的整个 Array 转换为单个字符串,其中每一行由;分隔。 . . Finally to remove the last ; finally 删除最后一个; generated via LEFT so we can use TEXTSPLIT to convert the result to a column array with the expected format.通过LEFT生成,因此我们可以使用TEXTSPLIT将结果转换为具有预期格式的列数组。

It is a little bit tricky but if you do it step by step you can test it until you get the expected result.这有点棘手,但如果你一步一步地做,你可以测试它,直到你得到预期的结果。 I don't think Excel provides a generic map function to convert from one format to another in the way you want it.我不认为 Excel 提供通用的 map function 以您想要的方式从一种格式转换为另一种格式。

示例 excel 文件

Disclaimer : I think the formula can be simplified using MAP function, so probably there is no need to deal with unnecessary delimiters created.免责声明:我认为可以使用MAP function 简化公式,因此可能不需要处理创建的不必要的分隔符。 I will update the answer in case it works.如果它有效,我将更新答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM