Search code examples
google-sheetsgoogle-apps-script

Is there a way to bulk change the name of a series of files located in a Google Spreadsheet?


I have a Google Spreadsheet where in cells B4:B there's a list of Google Drive URLs with the IDs of the files at then end of each URL (not the name and extension of the files). And in column N4:N I have a list of file names (without the extensions) that would be used to rename the files whose URLs in cellsB4:B are pointing to.
I would like to know if there is a script that can bulk rename the file names of each of the corresponding IDs into the names located in cellsN4:N while keeping the file extensions of each file.
When the column got empty in 5 consecutive rows, the script should stop.
Thank you so much.


Solution

  • SUGGESTION

    UPDATE: I've added another version of the script, which extracts the file IDs from the Google Drive URLs added on your list of links (as noted on your question)

    You can try these sample scripts:

    Version A: If only the fileIDs are on your source spreadsheet

    function bulkUpdateNames(){
      var sheets = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var links = sheets.getRange("B4:B").getValues();  // arrays of links
      var names = sheets.getRange("N4:N").getValues();  // arrays of new file names
    
      var blank_ctr = 0 // counter to check blank entries on the links column
    
      for (i = 0; i < links.length; i++){
        // if a link is found, changes the file name and resets the counter to 0
        if(links[i] != ""){
          DriveApp.getFileById(links[i]).setName(names[i]);
          blank_ctr = 0; // resets the counter
        }
        else{
          blank_ctr++;
        }
        // terminates the loop if 5 consecutive blanks are found
        if(blank_ctr == 5){
          break
        }
      }
    }
    

    Version B: If the Google Drive URLs are pasted on your source spreadsheet

    function bulkUpdateNames(){
      var sheets = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var links = sheets.getRange("B4:B").getValues().flat();  // arrays of links
      var names = sheets.getRange("N4:N").getValues();  // arrays of new file names
    
      var blank_ctr = 0 // counter to check blank entries on the links column
    
      for (i = 0; i < links.length; i++){
        // if a link is found, changes the file name and resets the counter to 0
        if(links[i] != ""){
          // splits the entire link and only retrieves the file ID
          file_id = links[i].split("/")[5]
          DriveApp.getFileById(file_id).setName(names[i]);
          blank_ctr = 0; // resets the counter
        }
        else{
          blank_ctr++;
        }
        // terminates the loop if 5 consecutive blanks are found
        if(blank_ctr == 5){
          break
        }
      }
    }
    

    The script will loop through the array of links and set a new file name for each designated link; a counter will increment for every blank cell on the link column and:

    1. Will reset if a link is found before 5 consecutive blanks are found, or;

    2. Will terminate the script once 5 consecutive blanks are found.

    OUTPUT

    image

    REFERENCES