0

This code works for me if I am importing two sheets:

={IMPORTRANGE("Sheet1Key","SheetName!A2:A500");IMPORTRANGE("Sheet2Key","SheetName!A2:A500")}

However, I have multiple sheets, all in the same folder, all with the data in the first sheet and all with the data in the same cells.

Is it possible to somehow either import from a folder or somehow add the IDs or names of all files within that folder automatically?

Community
  • 1
  • 1
Eoin
  • 1,413
  • 2
  • 17
  • 32
  • 2
    it could be possible only with a script. otherwise not – player0 Jun 28 '19 at 14:31
  • 2
    Possible duplicate of [Is it possible to do ImportRange in Google Apps Script?](https://stackoverflow.com/questions/11143684/is-it-possible-to-do-importrange-in-google-apps-script) – AMolina Jun 28 '19 at 15:08

2 Answers2

2

I am not sure what you mean by import all the files within that folder automatically. But what you could do is use the following code that I made a while ago. (You need to make it under scripts.google.com and you need to run the code) The only thing you would need to change is the folder name where I wrote “type name of folder”

function listFolderContents() {
var foldername = 'type name of folder';
var folderlisting = 'URL listing for folder ' + foldername;

var folders = DriveApp.getFoldersByName(foldername)
var folder = folders.next();
var contents = folder.getFiles();

var ss = SpreadsheetApp.create(folderlisting);
var sheet = ss.getActiveSheet();
sheet.appendRow( ['name', 'link'] );

var file;
var name;
var link;
var row;
while(contents.hasNext()) {
file = contents.next();
name = file.getName();
link = file.getUrl();
sheet.appendRow( [name, link] );     
}  
};

It will create a google spreadsheet called “URL listing for folder (name of folder) located inside the folder you are trying to get the files for. The spreadsheet will contain an array of all the file URL’s. It should be relatively easier to create the import range given the URL’s. You may be able to pick my code apart and use parts that help you with your goal. Hope this helps.

Sebastian
  • 108
  • 8
  • Welcome to stack, thanks, that looks good enough to start. I already have a sheet, but I guess I could use two sheets for this purpose. Cheers. – Eoin Jul 05 '19 at 16:58
  • In the line, var foldername = 'type name of folder'; What is the format for typing the name of the folder? Is it a URL or is it something like My Drive/Folder1/Folder2? I apologize if that is a dumb question. – Paul Bielaczyc Dec 11 '19 at 18:25
  • Hey sorry I didnt respond earlier. It is the the name of the folder as it appears in google drive. So if it appears as Folder1 then you would just use "Folder1". The example you gave "My Drive/Folder1/Folder2" would be the path for Folder2. Hope I clarified your question – Sebastian May 16 '20 at 20:12
1

You just need to use an array:

=query(arrayformula({importrange(sheet1!A1:Z100);importrange(sheet2!A1:Z100)}), "SELECT *",1)

This does assume that all data is structured the same.

zx485
  • 28,498
  • 28
  • 50
  • 59