Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

373
Visualizações
App script to get File Info (from a Google Drive Folder), Rename Files, Edit Permission controlled from a google sheet

What the app script code below does:

  1. function DriveLinkFetcher lists file details ("File url", "File name", "Viewers email", "Viewers names", "Editors email address", "Editors names", "Sharing Permission", "Sharing Access", "New file name", "Add new editors") of a google drive folder, given the url of the drive folder in cell "A2". The output is a list in a google sheet.

  2. function RenameDriveFiles renames "file names" in the folder to "new file names" depending on user input in the "New file name" column.


The problem:

function RenameDriveFiles works well for the files with no duplicate names. But if a file name has duplicates (for example, three "file 1", pls see the image) and I want to rename only the second file (rename to "file 100"), sometimes it renames all three files, sometimes two files.

The "File name" column contains duplicates and "File url" has unique IDs. So instead of using fldr.getFilesByName(oldNames[i]), is there a way to use fldr.getFilesByURL(URL[i])? Can you suggest any solution?


The output sheet is like this:

output sheet


The app script code:

    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var sh=ss.getActiveSheet();


        function DriveLinkFetcher() {
          
.....
.....
        }
    
    
    function RenameDriveFiles() {
    
      var url=sh.getRange("A2").getValue(); //url input from user
      var fldrID=url.match(/[-\w]{25,}/);
      var fldr=DriveApp.getFolderById(fldrID);
      
      //var files=fldr.getFiles();
      
      var lr=sh.getLastRow()-6;
      var oldNames = [];
      var newNames = [];
    
      oldNames = sh.getRange(7, 2, lr).getValues();
      newNames = sh.getRange(7, 9, lr).getValues();
    
      
        for(var i = 0; i < oldNames.length ; i++){
    
          var myFileIterator = fldr.getFilesByName(oldNames[i]);
    
          while(myFileIterator.hasNext()){
            var currentFile = myFileIterator.next();
            currentFile.setName(newNames[i]);  
    
        }
      } 
    }
    
    
    

I know some VBA but this is my first attempt in app script. I am learning. Any help, any tip how to make this code better will be much appreciated. Thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you already have the list of URLs the task (to rename the files) becomes extremely simply and straightforward:

var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getActiveSheet();

function RenameDriveFiles() {

  var url      = sh.getRange("A2").getValue(); //url input from user
  var fldrID   = url.match(/[-\w]{25,}/);
  var fldr     = DriveApp.getFolderById(fldrID);
  var lr       = sh.getLastRow() - 6;
  var urls     = sh.getRange(7, 1, lr).getValues().flat();
  var ids      = urls.map(u => u.split('/')[5]);
  var newNames = sh.getRange(7, 9, lr).getValues().flat();

  for (var n in newNames) {
    if (newNames[n] != '') DriveApp.getFileById(ids[n]).setName(newNames[n]);
  }
}

You don't even need the old names.

But there could be some problems with shared folders, actually.

Update

To add several editors from column 10 ('J'):

var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getActiveSheet();

function RenameDriveFiles() {

  var url      = sh.getRange("A2").getValue(); //url input from user
  var fldrID   = url.match(/[-\w]{25,}/);
  var fldr     = DriveApp.getFolderById(fldrID);
  var lr       = sh.getLastRow() - 6;
  var urls     = sh.getRange(7, 1, lr).getValues().flat();
  var ids      = urls.map(u => u.split('/')[5]);
  var newNames = sh.getRange(7, 9, lr).getValues().flat();
  var newEditors = sh.getRange(7, 10, lr).getValues().flat(); // column J

  for (var n in newNames) {
    var file = DriveApp.getFileById(ids[n]);
    var editors = newEditors[n].split(',');
    if (newNames[n] != '') file.setName(newNames[n]);
    if (editors[0] != '') editors.forEach(ed => file.addEditor(ed));
  }
}

I haven't tried it, I hope it works.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda