简体   繁体   中英

GAS: Combine Data from Multiple Google Sheets Documents (Workbooks) in Google Drive into One Workbook

I have multiple worksheets stored in a folder and what I would like to do is to read through every sheet (Specifically column D and H of every sheet) and append the data to a master file by mapping the columns to rows (The same way Google form does with responses on a google sheet).

The next step is to move the read files into another folder after they are read. I have done some digging and came up with the code below but am having a bit of challenge and will appreciate help to:

(a) Get the column data (D and H) to a single row on the master file. (b) Move the file copied into a different folder after they are read.

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  let combinedata = [];
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      combinedata.concat(data);
    }
  }
  ss.getRange(2,1,1,combinedata.length).setValues([combinedata]);
}

function getDataFromSpreadsheet(){
  var ss =  SpreadsheetApp.openById(ssID);
  var ws = ss.getSheets()[0]
  var data =  ws.getRange("A1:I" + ws.getLastRow()).getValues();
  data = data.reduce((acc,row) => acc.concat([row[3], row[7]]), []);
  return data;
}

Get specific columns:

You can use reduce to get the values from certain columns to a single array, which can then be added to your spreadsheet row:

function getDataFromSpreadsheet(ssID){
  var ss =  SpreadsheetApp.openById(ssID);
  var ws = ss.getSheets()[0]
  var data =  ws.getRange("A1:I" + ws.getLastRow()).getValues();// how do I get the columns I want here (D & H)
  data = data.reduce((acc,row) => acc.concat([row[3], row[7]]), []);
  return data;
}

Then, you would need to add this data to your main sheet. Considering that the data coming from different spreadsheets might have different lengths, I'd suggest calling appendRow inside the loop instead of calling setValues once outside (since, if all source columns are not the same length, you'd have to modify the resulting arrays to make them the same size; while this can be done and it could improve efficiency, I'm not sure it's worth it, specially if you don't have many spreadsheets):

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      ss.appendRow(data);
    }
  }
}

If all the columns have the same length , you can do this instead:

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  let combinedata = [];
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      combinedata.push(data);
    }
  }
  ss.getRange(2,1,combinedata.length, combinedata[0].length).setValues(combinedata);
}

Finally, if all the data coming from the different sheets should be in the same row , you could do this instead:

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  let combinedata = [];
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      combinedata.concat(data);
    }
  }
  ss.getRange(2,1,1,combinedata.length).setValues([combinedata]);
}

Move file:

For moving files to another folder, you just need to retrieve the file and the destination folder via DriveApp and then call:

const file = DriveApp.getFileById("FILE_ID");
const folder = DriveApp.getFolderById("FOLDER_ID");
file.moveTo(folder);

You have two questions.

How to move a File In Google Drive

You just need the fileID and the FolderID to move to. Both can be seen in the URL when browsing google drive.

在此处输入图像描述

Here's a function to carry it out:

function changeLocation(folderToMoveToID,fileID) {
  var destinationFolder = DriveApp.getFolderById(folderToMoveToID);
  var theFileToMove = DriveApp.getFileById(fileID);
  theFileToMove.moveTo(destinationFolder);
}

I'll try to answer other question later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM