这个示例脚本怎么样?我对你的问题的情况感兴趣。所以我对此提出了挑战。这也是为了我的学习。在我的回答中,我尝试使用 GAS 解决您的问题。我认为您的情况有几个答案。因此,请将此视为其中之一。
在此示例脚本中,“阶段 1”、“阶段 2”和“阶段 3”的值每 1 个数据周期创建一次。从您的问题和示例电子表格中,我认为数据的周期是 5。然后,将创建的值导入 3 张。
示例脚本:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var rawData = ss.getSheetByName("Raw Data").getDataRange().getValues();
var header = rawData[0];
var delimiter = "| "; // Please input delimiter.
var cycle = 5; // From your question, I thought the cycle of data is 5.
var phase1 = [];
var phase2 = [];
var phase3 = [];
rawData.shift();
for (var i = 0; i < rawData.length; i += cycle) {
var temp1 = [];
var temp2 = [];
for (var j = i; j < i + cycle; j++) {
temp1.push([rawData[j][0], rawData[j][1]]);
temp2.push([rawData[j][3], rawData[j][4]]);
}
var converted = temp2[0].map(function(_, i){return temp2.map(function(f){return f[i]})}) // Transpose
.map(function(e, i){return temp1[i].concat(e).concat(header[i + 3])}); // Add T1 and T2
// Create value for phase1.
Array.prototype.push.apply(phase1, converted);
// Create value for phase2.
phase2.push([converted[0].slice(0, 7).join(delimiter), converted[1].slice(0, 7).join(delimiter)]);
// Create value for phase3.
phase3.push([converted[0][0], header[3], header[4]]);
phase3.push(["", converted[0].slice(1, 7).join(delimiter), converted[1].slice(1, 7).join(delimiter)]);
phase3.push(["", "", ""]);
}
// If you want to change the sheet name, please modify this part.
var all = [
[ss.getSheetByName("Phase 1"), phase1],
[ss.getSheetByName("Phase 2"), phase2],
[ss.getSheetByName("Phase 3(Desired Final Output)"), phase3]
];
all.forEach(function(e) {
// Import values to 3 sheets.
e[0].getRange(e[0].getLastRow() + 1, 1, e[1].length, e[1][0].length).setValues(e[1]);
});
}
如果我误解了你的问题,我很抱歉。
编辑
- 运行修改后的脚本时,阶段 1、阶段 2 和阶段 3 的 3 张工作表的所有转换值都将被覆盖。
-
Data Melt 的名称不能用作函数名称。所以DataMelt 被用于它。
- 例如,
=DataMelt('Raw Data'!A2:E6) 被放入单元格,第 1 阶段的转换数据被导入。
- 在这个函数中,可以使用5行1周期的数据。
修改后的脚本
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var rawData = ss.getSheetByName("Raw Data").getDataRange().getValues();
var header = rawData[0];
var delimiter = "| "; // Please input delimiter.
var cycle = 5; // From your question, I thought the cycle of data is 5.
var phase1 = [];
var phase2 = [];
var phase3 = [];
rawData.shift();
for (var i = 0; i < rawData.length; i += cycle) {
var temp1 = [];
var temp2 = [];
for (var j = i; j < i + cycle; j++) {
temp1.push([rawData[j][0], rawData[j][1]]);
temp2.push([rawData[j][3], rawData[j][4]]);
}
var converted = temp2[0].map(function(_, i){return temp2.map(function(f){return f[i]})}) // Transpose
.map(function(e, i){return temp1[i].concat(e).concat(header[i + 3])}); // Add T1 and T2
// Create value for phase1.
Array.prototype.push.apply(phase1, converted);
// Create value for phase2.
phase2.push([converted[0].slice(0, 7).join(delimiter), converted[1].slice(0, 7).join(delimiter)]);
// Create value for phase3.
phase3.push([converted[0][0], header[3], header[4]]);
phase3.push(["", converted[0].slice(1, 7).join(delimiter), converted[1].slice(1, 7).join(delimiter)]);
phase3.push(["", "", ""]);
}
phase1.unshift(["Col A", "Col B", "Catalogue", "Display", "Equivalent Single Price", "In-Store_Shopper", "Mechanic", "Time"]);
phase2.unshift(["T1", "T2"]);
var all = [
[ss.getSheetByName("Phase 1"), phase1],
[ss.getSheetByName("Phase 2"), phase2],
[ss.getSheetByName("Phase 3(Desired Final Output)"), phase3]
];
all.forEach(function(e) {
// Import values to 3 sheets.
e[0].getRange(1, 1, e[1].length, e[1][0].length).setValues(e[1]);
});
}
// Added new function
function DataMelt(e) {
var e = [["Chalk","A0C-Len Superior Kids TP 80gm","Catalogue","No","Yes"],["Chalk","A0C-Len Superior Kids TP 80gm","Display","Shelf","GE"],["Chalk","A0C-Len Superior Kids TP 80gm","Equivalent Single Price",2.49,2.49],["Chalk","A0C-Len Superior Kids TP 80gm","In-Store_Shopper","",""],["Chalk","A0C-Len Superior Kids TP 80gm","Mechanic","LDLP","Off"]];
var rawData = e;
var header = ["Col A", "Col B", "Data Type", "T1", "T2"];
var cycle = 5; // From your question, I thought the cycle of data is 5.
var phase1 = [];
var temp1 = [];
var temp2 = [];
for (var j = 0; j < cycle; j++) {
temp1.push([rawData[j][0], rawData[j][1]]);
temp2.push([rawData[j][3], rawData[j][4]]);
}
var converted = temp2[0].map(function(_, i){return temp2.map(function(f){return f[i]})}) // Transpose
.map(function(e, i){return temp1[i].concat(e).concat(header[i + 3])}); // Add T1 and T2
// Create value for phase1.
Array.prototype.push.apply(phase1, converted);
return phase1;
}