我试图产生一个“反向枢轴”功能。我已经长时间努力寻找这样的功能,但找不到已经存在的功能。
我有一个摘要表,其中包含最多20列和数百行,但我想将其转换为平面列表,以便我可以导入到数据库(甚至使用平面数据来创建更多数据透视表!)
所以,我有这种格式的数据:
| Customer 1 | Customer 2 | Customer 3
----------+------------+------------+-----------
Product 1 | 1 | 2 | 3
Product 2 | 4 | 5 | 6
Product 3 | 7 | 8 | 9
并需要将其转换为以下格式:
Customer | Product | Qty
-----------+-----------+----
Customer 1 | Product 1 | 1
Customer 1 | Product 2 | 4
Customer 1 | Product 3 | 7
Customer 2 | Product 1 | 2
Customer 2 | Product 2 | 5
Customer 2 | Product 3 | 8
Customer 3 | Product 1 | 3
Customer 3 | Product 2 | 6
Customer 3 | Product 3 | 9
我已经创建了一个函数,它将从sheet1
读取范围并在同一个工作表的底部附加重新格式化的行,但是我试图让它工作,所以我可以在sheet2
上使用该函数来读取整个范围sheet1
。
无论我尝试什么,我似乎无法让它工作,并想知道是否有人可以给我任何指针?
这是我到目前为止:
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
heads = values[0]
for (var i = 1; i <= numRows - 1; i++) {
for (var j = 1; j <= values[0].length - 1; j++) {
var row = [values[i][0], values[0][j], values[i][j]];
sheet.appendRow(row)
}
}
};
我写了一个简单的通用自定义函数,它是100%可重用的,你可以解开/反转任何大小的表。
在你的情况下你可以像这样使用它:=unpivot(A1:D4,1,1,"customer","sales")
因此,您可以像使用电子表格中的任何内置数组函数一样使用它。
以下是来源:
/**
* Unpivot a pivot table of any size.
*
* @param {A1:D30} data The pivot table.
* @param {1} fixColumns Number of columns, after which pivoted values begin. Default 1.
* @param {1} fixRows Number of rows (1 or 2), after which pivoted values begin. Default 1.
* @param {"city"} titlePivot The title of horizontal pivot values. Default "column".
* @param {"distance"[,...]} titleValue The title of pivot table values. Default "value".
* @return The unpivoted table
* @customfunction
*/
function unpivot(data,fixColumns,fixRows,titlePivot,titleValue) {
var fixColumns = fixColumns || 1; // how many columns are fixed
var fixRows = fixRows || 1; // how many rows are fixed
var titlePivot = titlePivot || 'column';
var titleValue = titleValue || 'value';
var ret=[],i,j,row,uniqueCols=1;
// we handle only 2 dimension arrays
if (!Array.isArray(data) || data.length < fixRows || !Array.isArray(data[0]) || data[0].length < fixColumns)
throw new Error('no data');
// we handle max 2 fixed rows
if (fixRows > 2)
throw new Error('max 2 fixed rows are allowed');
// fill empty cells in the first row with value set last in previous columns (for 2 fixed rows)
var tmp = '';
for (j=0;j<data[0].length;j++)
if (data[0][j] != '')
tmp = data[0][j];
else
data[0][j] = tmp;
// for 2 fixed rows calculate unique column number
if (fixRows == 2)
{
uniqueCols = 0;
tmp = {};
for (j=fixColumns;j<data[1].length;j++)
if (typeof tmp[ data[1][j] ] == 'undefined')
{
tmp[ data[1][j] ] = 1;
uniqueCols++;
}
}
// return first row: fix column titles + pivoted values column title + values column title(s)
row = [];
for (j=0;j<fixColumns;j++) row.push(fixRows == 2 ? data[0][j]||data[1][j] : data[0][j]); // for 2 fixed rows we try to find the title in row 1 and row 2
for (j=3;j<arguments.length;j++) row.push(arguments[j]);
ret.push(row);
// processing rows (skipping the fixed columns, then dedicating a new row for each pivoted value)
for (i=fixRows;i<data.length && data[i].length > 0 && data[i][0];i++)
{
row = [];
for (j=0;j<fixColumns && j<data[i].length;j++)
row.push(data[i][j]);
for (j=fixColumns;j<data[i].length;j+=uniqueCols)
ret.push(
row.concat([data[0][j]]) // the first row title value
.concat(data[i].slice(j,j+uniqueCols)) // pivoted values
);
}
return ret;
}
这基本上是数组操作......下面是一个代码,可以执行您想要的操作并将结果写回现有数据下面。
如果您愿意,您当然可以根据新表格进行调整。
function transformData(){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();//read whole sheet
var output = [];
var headers = data.shift();// get headers
var empty = headers.shift();//remove empty cell on the left
var products = [];
for(var d in data){
var p = data[d].shift();//get product names in first column of each row
products.push(p);//store
}
Logger.log('headers = '+headers);
Logger.log('products = '+products);
Logger.log('data only ='+data);
for(var h in headers){
for(var p in products){ // iterate with 2 loops (headers and products)
var row = [];
row.push(headers[h]);
row.push(products[p]);
row.push(data[p][h])
output.push(row);//collect data in separate rows in output array
}
}
Logger.log('output array = '+output);
sheet.getRange(sheet.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}
自动将结果写入新工作表中用以下代码替换最后一行代码:
var ns = SpreadsheetApp.getActive().getSheets().length+1
SpreadsheetApp.getActiveSpreadsheet().insertSheet('New Sheet'+ns,ns).getRange(1,1,output.length,output[0].length).setValues(output);
我不认为你有足够的数组公式答案,所以这是另一个。
测试数据(表1)
客户配方
=ArrayFormula(hlookup(int((row(indirect("1:"&Tuples))-1)/Rows)+2,{COLUMN(Sheet1!$1:$1);Sheet1!$1:$1},2))
(使用一些数学运算使其重复并使用hlookup在列标题中找到正确的列)
产品配方
=ArrayFormula(vlookup(mod(row(indirect("1:"&Tuples))-1,Rows)+2,{row(Sheet1!$A:$A),Sheet1!$A:$A},2))
(使用mod和vlookup在行标题中找到正确的行的类似方法)
数量公式
=ArrayFormula(vlookup(mod(row(indirect("1:"&Tuples))-1,Rows)+2,{row(Sheet1!$A:$A),Sheet1!$A:$Z},int((row(indirect("1:"&Tuples))-1)/Rows)+3))
(上述方法的扩展,在2d数组中查找行和列)
然后将这三个公式组合成一个查询,以过滤掉数量的任何空白值
=ArrayFormula(query(
{hlookup(int((row(indirect("1:"&Tuples))-1)/Rows)+2, {COLUMN(Sheet1!$1:$1);Sheet1!$1:$1},2),
vlookup(mod(row(indirect("1:"&Tuples))-1,Rows)+2,{row(Sheet1!$A:$A),Sheet1!$A:$A},2),
vlookup(mod(row(indirect("1:"&Tuples))-1,Rows)+2,{row(Sheet1!$A:$A),Sheet1!$A:$Z},int((row(indirect("1:"&Tuples))-1)/Rows)+3)},
"select * where Col3 is not null"))
注意
命名范围Rows和Cols是从第一列获得的,使用counta和Tuples的数据行是它们的产品。单独的公式
=counta(Sheet1!A:A)
=counta(Sheet1!1:1)
和
=counta(Sheet1!A:A)*counta(Sheet1!1:1)
如果需要,可以包括在主要公式中,但可读性有所损失。
作为参考,这里是适用于当前情况的“标准”拆分/连接解决方案(具有50K数据限制):
=ArrayFormula(split(transpose(split(textjoin("♫",true,transpose(if(Sheet1!B2:Z="","",Sheet1!B1:1&"♪"&Sheet1!A2:A&"♪"&Sheet1!B2:Z))),"♫")),"♪"))
这也相当慢(处理2401个数组元素)。如果将计算限制为数据的实际维度,则对于小型数据集来说要快得多:
=ArrayFormula(split(transpose(split(textjoin("♫",true,transpose(if(Sheet1!B2:index(Sheet1!B2:Z,counta(Sheet1!A:A),counta(Sheet1!1:1))="","",Sheet1!B1:index(Sheet1!B1:1,counta(Sheet1!1:1))&"♪"&Sheet1!A2:index(Sheet1!A2:A,counta(Sheet1!A:A))&"♪"&Sheet1!B2:index(Sheet1!B2:Z,counta(Sheet1!A:A),counta(Sheet1!1:1))))),"♫")),"♪"))
Here is a demo file使用内置自定义函数和数组公式的方法:
=COUNTA(data!A:A)
计算行数。
A2:=COUNTA(data!1:1)
计算列数。
A3:=CELL("address",data!A1)
中级步骤。
A4:=LEFT(A3,FIND("!",A3)-1)
使用源数据计算工作表的名称。=ArrayFormula( VLOOKUP( MOD(ROW(INDIRECT("A1:A"&Aux!A1*Aux!A2))-1,Aux!A1)+1+1, {(ROW(INDIRECT("A1:A"&Aux!A1+1))),INDIRECT(Aux!A4&"!R1C1:R"&Aux!A1+1&"C"&Aux!A2+1,false)}, 2 ) )
B1:列标题 B2:
=ArrayFormula( VLOOKUP( SIGN(ROW(INDIRECT("A1:A"&Aux!A1*Aux!A2))), {(ROW(INDIRECT("A1:A"&Aux!A1+1))),INDIRECT(Aux!A4&"!R1C1:R"&Aux!A1+1&"C"&Aux!A2+1,false)}, MOD(ROW(INDIRECT("A1:A"&Aux!A1*Aux!A2))-1,Aux!A2)+1+2 ) )
C1:值 C2:
=ArrayFormula( VLOOKUP( MOD(ROW(INDIRECT("A1:A"&Aux!A1*Aux!A2))-1,Aux!A1)+1+1, {(ROW(INDIRECT("A1:A"&Aux!A1+1))),INDIRECT(Aux!A4&"!R1C1:R"&Aux!A1+1&"C"&Aux!A2+1,false)}, MOD(ROW(INDIRECT("A1:A"&Aux!A1*Aux!A2))-1,Aux!A2)+1+2 ) )
主要构造的描述
ROW(INDIRECT("A1:A"&Aux!A1*Aux!A2)
返回一系列连续数字,其高度与所需的最终结果相同。{(ROW(INDIRECT("A1:A"&Aux!A1+1))),INDIRECT(Aux!A4&"!R1C1:R"&Aux!A1+1&"C"&Aux!A2+1,false)}
返回一个数组,第一列包含行索引,下一列是源数据。如果您的数据只有一个唯一的密钥列,那么this spreadsheet可能会满足您的需求。
您的unpivot表将包含:
=OFFSET(data!$A$1,INT((ROW()-2)/5)+1,0)
=OFFSET(data!$A$1,0,IF(MOD(ROW()-1,5)=0,5,MOD(ROW()-1,5)))
=INDEX(data!$A$1:$F$100,MATCH(A2,data!$A$1:$A$100,FALSE),MATCH(B2,data!$A$1:$F$1,FALSE))
其中5
是要转移的列数。
我没有制作电子表格。我在相同的搜索中碰巧遇到了这个问题。
使用array.reduce
和array.splice
进行数组操作 - 简约方法:
/**
* Unpivots the given data
*
* @return Unpivoted data from array
* @param {A1:F4} arr 2D Input Array
* @param {3} numCol Number of static columns on the left
* @param {A1:C1} headers [optional] Custom headers for output
* @customfunction
*/
function unpivot(arr, numCol, headers) {
var out = arr.reduce(function(acc, row) {
var left = row.splice(0, numCol); //static columns on left
row.forEach(function(col, i) {
acc.push(left.concat([acc[0][i + numCol], col])); //concat left and unpivoted right and push as new array to accumulator
});
return acc;
}, arr.splice(0, 1));//headers in arr as initial value
headers ? out.splice(0, 1, headers[0]) : null; //use custom headers, if present.
return out;
}
用法:
=UNPIVOT(A1:F4,1,{A1,"Month","Sales"})//Outputs 1 static and 2 unpivoted columns from 1 static and 4+ pivoted columns