Skip to content

Moving file appscript -> Hyperlink move to another folder – Google App Script

I am trying with all my energy to create a code where I get a hyperlink of a file (uploaded to folder in drive) and move this file depending on a conditionn (switch case statement).

When I dissect the code and create subparts every subpart works. Am Stuck.

function onEdit(e) {
  
// defining variables. Basicly getting some values and ranges of a spreadsheet.

  var row = e.range.getRow();
  var col = e.range.getColumn();
  var repet = e.source.getActiveSheet().getRange(row,11).getValue();
  var rang_arquiv = e.source.getActiveSheet().getRange(row,20);
  var arquiv = rang_arquiv.getValue();
  var identif = e.source.getActiveSheet().getRange(row,20).getRichTextValue().getLinkUrl().match(/[-w]{25,}/);
  var fornecedor = e.source.getActiveSheet().getRange(row,19).getValue();
  
// switch statemnt used to define a folder ID adress (end). Depending on a value of the spreadsheet I will move to a specific folder.


  switch (fornecedor)  {
        case "ABC":
            {
              end = "1NsMaEwDiW8EOWUD5MNB6GGE5kOCJJlqh";
            break;
            }
        case "PLATE":
            {
              end = "1pZJXTinCNCBU3t1Wyal5AUU-lARKpW71";
            break;
            }
        case "DIGICERTA":
            {
              end = "1qo_cYe7rAhj4wN2bM_MqNkl1eXK21YP1";
            break;
            }
        case "FOTOGRAV":
            {
              end = "1-vOoRpBLdwZjAJ_ZA2srOAvZOJ6ccge8";
            break;
            }
        case "REGRAVAÇÃO":
            {
            break;
            }
        case "REPOSIÇÃO":
            {
            break;
            }
        
  }

// An if statement to verify some conditions

  if(repet === "Novo" && col === 21 && e.source.getActiveSheet().getName() === "Fluxo Sangar" && row > 2  && arquiv != false && (e.oldValue === "false" && e.value === "TRUE")){ 

// here i wantr to get the file and move it. Already tried creating these var outside the if statement

  var file = DriveApp.getFileById(identif);
  var folder = DriveApp.getFolderById(end);
  file.makeCopy(folder);

// After moving the file write the timestamp

  e.source.getActiveSheet().getRange(row,22).setValue(identif);

  }

Answer

You can’t do it with onEdit() trigger. Simply triggers can operate only within the current spreadsheet and its data.

https://developers.google.com/apps-script/guides/triggers

Simple Triggers

They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.

They can modify the file they are bound to, but cannot access other files because that would require authorization.