1

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);

  }
2
  • Apologies, but I can't understand your issue here. Can you edit your post and include the behavior your script should be and provide a sample spreadsheet so we can test your code? Also, what do you mean by subparts and how do you check every subpart works? Commented Sep 2, 2021 at 17:56
  • The answer bellow did it for me. When I´ve said subparts I meant that I created a simple function (myFunction) and worked every single process the code should do and run them individually. ( I am a novice in coding so don´t know how debbug properly). tks anyway mate Commented Sep 3, 2021 at 13:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Was getting crazy with this issue. I´ll try to call a function and see if work out. ( i´ve tried to mark your answer as useful but couldnt because of lack of reputation :/ )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.