Home > Net >  Replace Reference DOCS file name from Sheet . file
Replace Reference DOCS file name from Sheet . file

Time:09-22

Here is my example:

Sheet file with ID:
1uxEy8cPkNQVBVAFSHPBhvq0aRgRVZ0z2_I1AxK0gcuI

There are 2 columns: Column A: contains the word to be replaced, for example:

"A1": Le Minh Hai, "A2": ABC, "A3": HAIBA

Column B: contains the word you want to replace, for example:

"B1": Nguyen Le Minh, "B2": CDE, "B3": BAAAA

There is a file named

This is the file of Le Minh Hai

I want it to be changed to:

This is Nguyen Le Minh's file

If there is a file with a different name

This is the ABC file

It will change back to

This is a CDE file

Same for other files

Code:

function getNewestFileInFolder() {
  var arryFileDates,file,fileDate,files,folder,folders,
      newestDate,newestFileID,objFilesByDate;

  folders = DriveApp.getFoldersByName('bvm-hd');  
  while (folders.hasNext()) {
    folder = folders.next();
    files = folder.getFilesByType("application/vnd.google-apps.document");
    while (files.hasNext()){
      file = files.next();
  var newestFileID = file.getId();
  var currentDoc = DocumentApp.openById(newestFileID);
  var ss = SpreadsheetApp.openById('1t9T5FWedPLFcQ6wqcx4gGRHrrpldZHcLhgBJR0JbrPI');//ID sheet find and replace
  SpreadsheetApp.setActiveSpreadsheet(ss);
  SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);
  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Trang tính1');
  var dongcuoi= sh.getLastRow();
  var dc = dongcuoi  1;
  var doc = DocumentApp.openById(newSp.getId());
  var rgtxt = doc.getName();
  var rgrep = ss.getSheets()[0].getRange("A1:B" dc);
  var repA = rgrep.getValues().filter(r => r.every(c => c.toString()));
  repA.forEach(e => rgtxt.replaceText(...e));      
  }
    }
  
}

CodePudding user response:

I believe your goal is as follows.

  • You want to rename the filename of Google Document in the specific folder using Spreadsheet.
  • You want to change the existing filename including the values of column "A" to the values of column "B".
  • For example, when the filename is This is the file of Le Minh Hai, you want to change it to This is Nguyen Le Minh's file.

In this case, how about the following modified script?

Modified script:

function getNewestFileInFolder() {
  var ss = SpreadsheetApp.openById('1t9T5FWedPLFcQ6wqcx4gGRHrrpldZHcLhgBJR0JbrPI');
  var sheet = ss.getSheetByName('Trang tính1');
  var values = sheet.getDataRange().getValues();
  var folders = DriveApp.getFoldersByName('bvm-hd');
  while (folders.hasNext()) {
    var folder = folders.next();
    var files = folder.getFilesByType("application/vnd.google-apps.document");
    while (files.hasNext()) {
      file = files.next();
      var filename = file.getName();
      var check = values.filter(([a]) => filename.includes(a));
      if (check.length > 0) {
        file.setName(filename.replace(check[0][0], check[0][1]));
      }
    }
  }
}
  • From your script, I'm worried that there might be the folders of folder name of bvm-hd. So I use the while loop for retrieving the folders.
  • The values can be retrieved from Spreadsheet by one call. So in this modified script, this script is moved to the out of loops.
  • In order to change the filename, you can use getName and setName of Class File.

References:

  • Related