【发布时间】:2014-09-17 05:58:10
【问题描述】:
我正在尝试生成“反向枢轴”功能。我已经为这样的功能搜索了很长时间,但找不到已经存在的功能。
我有一个包含多达 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)
}
}
};
【问题讨论】:
标签: google-apps-script google-sheets pivot-table unpivot