我已经在这里看到一些答案,但没有一个适用于 Mac 版 Excel 2016。
我想要一个简单的解决方案来将矩阵转换为这样的列表:
date city1 city2 cityN
jan1 value value value
jan2 value value value
到
date city value
jan1 city1 value
jan1 city2 value
jan1 cityN value
jan2 city1 value
jan2 city2 value
jan2 cityN value
答案1
我使用了 google 电子表格脚本:
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);
var ns = SpreadsheetApp.getActive().getSheets().length+1
SpreadsheetApp.getActiveSpreadsheet().insertSheet('New Sheet'+ns,ns).getRange(1,1,output.length,output[0].length).setValues(output);
}