Home > Enterprise >  Merge Data From Multiple Sheets Onto 1 Sheet
Merge Data From Multiple Sheets Onto 1 Sheet

Time:02-01

I'm in the process of creating a script that'll merge data from multiple Google sheets onto one.

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.

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

Here's the code for the script.

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.

CodePudding user response:

You can do this without scripts using 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)

CodePudding user response:

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);
    }
  })
}
  • Related