Search code examples
google-apps-scriptgoogle-appsheet

Nothing happen when run a script when a trigger an action from AppSheet


I followed this guide, to rename file in Google Drive when a trigger an action from AppSheet, but Google Drive file names don't change:

https://www.googlecloudcommunity.com/gc/Tips-Tricks/How-to-control-or-rename-uploaded-files-in-a-fil...

This is the last execution, apparently there are no errors

But it should change the file names in Google Drive.

see image attached.

**And this is my custom code: **


function renameFile(coec_cedula, coec_nombres,  coec_apellidos) {
  //Log Request
  console.log(`Received request for ${coec_cedula} with Cheque No ${coec_nombres} and amount of ${coec_apellidos}`);

  let folder = DriveApp.getFolderById("1qP-Jv7OWxTlLCaxH9HpFmQC7R20Ew6bm"); 
  let subfolder = folder.getFoldersByName(coec_cedula);
  
   
  if(subfolder.hasNext()) {
    let subfolderObj = subfolder.next();
    let subfolderId = subfolderObj.getId();
    
    // Get the folder id of the subfolder created via the [FILE] column's Image/File Path
    let coec_cedulaFolder = DriveApp.getFolderById(subfolderId);
    
    // Get all the files inside the folder
    let files = coec_cedulaFolder.getFiles();
    let file;

    // Loop through each file in the folder
    while (files.hasNext()) {
      file = files.next();
      filename = file.getName();
      
      if (filename.includes( coec_nombres )) {

        // get the file extension of the uplaoded file
        let ext = filename.split(".").pop();

        console.log( filename );
        // Rename the file
        let newName = `${coec_cedula}.${ext}`;
        
        //Rename the file in Google Drive
        file.setName(newName);
        
        let newValue = `/CWA_OEC_ApprovedData_Files/${coec_cedula}/${newName}`;
        
        return newValue;  // Return the new value to AppSheet
      } 
            
    }  // while files.hasNext()

  } // if subfolder.hasNext()
} // end function renameFile

image atached


Solution

  • I modified it so that it figures out the real path for you but I found that the file is getting renamed properly.

    function myFunk01(a = "folder11", b = "1", c = "1") {
      console.log(`Received request for ${a} with Cheque No ${b} and amount of ${c}`);
      let folder = DriveApp.getFolderById("gobj.globals.testid");
      let subfolder = folder.getFoldersByName(a);
      if (subfolder.hasNext()) {
        let subfolderObj = subfolder.next();
        let fid = subfolderObj.getId();
        let cfldr = DriveApp.getFolderById(fid);
        let files = cfldr.getFiles();
        while (files.hasNext()) {
          let file = files.next();
          let filename = file.getName();
          if (filename.includes(b)) {
            let ext = filename.split(".").pop();
            Logger.log(filename);
            let newName = `${a}.${ext}`;
            file.setName(newName);
            var pA = [];//path array 
            pA.push(file.getName());
            var fo = file.getParents();
            while (fo.hasNext()) {
              let f = fo.next();
              pA.push(f.getName());
              fo = f.getParents()
            }
            var path = pA.reverse().join(' / ');
            let newValue = path;//includes file name
            Logger.log(newValue);
            return newValue;
          }
        }
      }
    }