Home > Mobile >  How to copy individual files into folder directory at relevant search depth reading from Google Shee
How to copy individual files into folder directory at relevant search depth reading from Google Shee

Time:02-16

I managed to find a way to create a folder directory from reading a Google Sheet linked below.

https://docs.google.com/spreadsheets/d/1qgDd8PEmHSYz5IsN9banYBjbmAyoLLVe3WMnOvmHdlE/edit#gid=0

Now, my predicament is to transfer individual files into those newly created folders and the relevant search Depth. I thought this would be possible; however, I am now thinking it isn't hence the limited code below.

This is because for each file (Column C), I would need to read the source parent folder ID (Column D) and then convert this to the destination folder equivalent recorded in Column F. I'm not sure how I can go about doing this? Moreover, should the Google Sheet be sorted into folders and files in a first instance (Column C), ideally the solution should still work when files and folders are out of order.

I know this is not the best solution; however, I am looking for a solution I can understand in order to advance my understanding.

function copyFiles() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName('Details');
  const range = ws.getDataRange();
  const values = range.getValues();
  values.forEach(function(row, index) {
    if (row[2] == 'File'){

    } else {

    }
    })
}

Thank you in advance

CodePudding user response:

Assuming your data is well organized sequentially (based on files'id in column B)

function copyFiles() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName('Sheet1');
  const range = ws.getDataRange();
  const values = range.getValues();
  var parentID = ''
  values.forEach(r => {
    if (r[2] == 'Folder') { parentID = r[5] }
    else if (r[2] == 'File') {
      r[5] = DriveApp.getFileById(r[1]).makeCopy(r[0], DriveApp.getFolderById(parentID)).getId()
    }
  })
  range.setValues(values)
}
  • Related