1

Here is the link with an example:

https://docs.google.com/spreadsheets/d/1qNN3G4c9FNGk3bwKXJODUuIQIe5dRfVOwu8M7ZgU384/edit#gid=329910889

I want to combine the data from the "First","Second" and "Third" tab into the "Combined" tab all under the same Headers (Title,Type,Genre)

Please let me know if there is a script for this - as the actual doc I need to use it on has more than 30 tabs and a formula would potentially crash.

Marios
  • 26,333
  • 8
  • 32
  • 52
BabyYoda
  • 79
  • 4

1 Answers1

3

Explanation:

  • The following script uses forEach to iterate over the sheets, excluding the ones mentioned in the exclude_sheets array.

  • For every sheet, it stores the values, between particular ranges that you need to setup in the beginning of the script, to the data array.

  • The range starts from the 6th row (start_row) between column A (start_col=1) and column C (end_col = 3). The end row is defined as the last row with content, so you don't need to manually set it up and it can be also different between the sheets. However, the starting row and column ranges must be the same between the sheets.

  • It finally pastes the data to the Test sheet (res_sheet = 'Test') starting from the cell A2.

Feel free to modify the parameters above but respect the underlying logic, otherwise the script won't work properly.


Google Apps Script Solution:

function myFunction() {
  
  const ss = SpreadsheetApp.getActive();
  const sheets = ss.getSheets();
  const exclude_sheets = ['Combined','Test'];
  const res_sheet = 'Test';
  const start_row = 6;
  const start_col = 1;
  const end_col = 3;
  
  const data = []
  sheets.forEach(sh=>{
                 
  if(!exclude_sheets.includes(sh.getName())){
  let temp = sh.getRange(start_row,start_col,sh.getLastRow()-start_row+1,end_col).getValues()
  data.push(...temp);
  }});

 ss.getSheetByName(res_sheet).getRange(2,1,data.length,data[0].length).setValues(data);
 
}

Please make sure that V8 runtime enviroment is enabled before you use this solution.


References:

Marios
  • 26,333
  • 8
  • 32
  • 52
  • Hey Marios, thanks for this. I tried using it to my actual doc with only changing the tab names from the exclude and res sheet and I am getting this error: Exception: The number of rows in the range must be at least 1. (line 77, file "onOpen") Any idea what's wrong here? I haven't adjusted anything else (I have enabled V8) – BabyYoda Oct 09 '20 at 15:14
  • 1
    @Christina where did I use `onOpen`? Please go to Views = > Script editor and run `myFunction`. I added it to your file. http://prntscr.com/uw8i1p . The data will be added to the `Test` sheet starting from the second row ( this is why I cleared that sheet except for the title). – Marios Oct 09 '20 at 15:18
  • 1
    @Christina If you want to execute this function from an `onOpen` menu then let me know. However, keep in mind that you need to provide me your full code because if you already use an `onOpen` function I would have to adjust it to work properly. – Marios Oct 09 '20 at 15:24
  • the doc I shared is just an example sheet I created. I pasted the code in the actual doc (which I cannot share) and on that one I have many more functions - onOpen is just the file's name but I used your exact code and I am getting that error (only change I made is in the tab Names for exclude and res) Given that I cannot share the original doc with you - any idea where the error may be coming from? – BabyYoda Oct 09 '20 at 15:26
  • 1
    @Christina the error indicates that one of the sheets has 0 entries in the range I explained in my answer. **Check** if every sheet has data in the range `A6:C`. – Marios Oct 09 '20 at 15:28