[英]Merge Data From Multiple Sheets Onto 1 Sheet
I'm in the process of creating a script that'll merge data from multiple Google sheets onto one.我正在创建一个脚本,将多个 Google 工作表中的数据合并到一个。
As I'm new to this, I'm having trouble figuring out an error I keep getting.由于我是新手,所以我无法找出我不断遇到的错误。 It states 'Exception: Range not found' in line 26 of the script.它在脚本的第 26 行指出“异常:未找到范围”。
I'm assuming, maybe incorrectly that there's something wrong with the range I'm using in my variables as to which data to merge onto the main sheet.我假设,也许错误地认为我在变量中使用的范围有问题,关于哪些数据要合并到主表上。 But changing that doesn't seem to help.但改变它似乎并没有帮助。
Any tips, advice, or things to try is greatly appreciated.非常感谢任何提示、建议或尝试。
Here's a link to the sheets.这是工作表的链接。 https://docs.google.com/spreadsheets/d/1hv9zANMWzDBpKDWQ3hbDjX4nezEtOoptFbY0PYn_c5Y/edit?usp=sharing https://docs.google.com/spreadsheets/d/1hv9zANMWzDBpKDWQ3hbDjX4nezEtOoptFbY0PYn_c5Y/edit?usp=sharing
Here's the code for the script.这是脚本的代码。
function function
myFunction() {
//FILTER({'Workout1'}) !E15:AA26,"DATA TAB 1 - ROW "&ROW('Workout1'!E15:E26)},'Workout1'!E15:E26<>"");
//FILTER({'Workout2'}) !E15:AA26,"DATA TAB 2 - ROW "&ROW('Workout2'!E15:E26)},'Workout2'!E15:E26<>"");
//FILTER({'Workout3'}) !E15:AA26,"DATA TAB 3 - ROW "&ROW('Workout3'!E15:E26)},'Workout3'!E15:E26"");
//FILTER({'Workout1'}) !E15:AA26,"DATA TAB 1 - ROW "&ROW('Workout1'!E15:E26)},'Workout1'!E15:E26<>"");
//setvariables
const masterSheet = "Data";
const ignoreSheets = ["Dashboard"];
const dataRange = "E15:AA26";
const checkRange = "E15:E26";
//end set variables
const ss = SpreadsheetApp.getActiveSpreadsheet();
const allsheets = ss.getSheets();
const filteredlistofsheets = allsheets.filter(s => ignoreSheets.indexOf(s.getSheetName()) == -1);
let formulaArray = filteredlistofsheets.map(s => `FILTER({'${s.getSheetName()}'$!{datarange},"${s.getSheetName()} - ROW "&ROW('${s.getSheetName()}!${checkRange},ROW "&ROW('${s.getSheetName()}'!${checkRange}<>"")`);
let formulaText = "={" + formulaArray.join(";") + "}";
ss.getSheetByName(masterSheet).getRange("masterSheetFormulaCell").setformula(formulaText);
}
Thanks very much.非常感谢。
You can do this without scripts using REDUCE .您可以使用REDUCE在没有脚本的情况下执行此操作。
=QUERY(
REDUCE({0,0,0,0},
{"Workout1",
"Workout2",
"Workout3"},
LAMBDA(acc,cur,
{acc;
INDIRECT(cur&"!E15:E26"),
BYROW(INDIRECT(cur&"!K15:O26"),LAMBDA(row,IFERROR(AVERAGE(row)))),
BYROW(INDIRECT(cur&"!Q15:U26"),LAMBDA(row,IFERROR(AVERAGE(row)))),
BYROW(INDIRECT(cur&"!W15:AA26"),LAMBDA(row,IFERROR(AVERAGE(row))))})
),
"select Col1,
sum(Col2),
sum(Col3),
avg(Col4)
where Col1 is not null
group by Col1
label Col1 'Exercise',
sum(Col2) 'Reps For Each Set',
sum(Col3) 'Weight Per Set (lbs.)',
avg(Col4) 'Rest Between Sets (sec.)'",1)
function mergeAllToFirst() {
const ss = SpreadsheetApp.openById("Spreadsheet ID");
let sht;
ss.getSheets().forEach((sh, i) => {
if (i == 0) {
sh.clearContents()
sht = sh;
} else {
let vs = sh.getDataRange().getValues();
sht.getRange(sht.getLastRow() + 1, 1).setValue(sh.getName());
sht.getRange(sht.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.