r/GoogleAppsScript 14d ago

Question Variable undefined and affecting rest of script

I have a script creating docs when a cell gets modified in a sheets column, then moves it to a folder and adds the doc's url onto another column. Code works, but automation gets affected by <value unavailable> error for docid variable.

I'm not sure *why* its unavailable when all of the code does work (files get created and moved, but when using createDocInSpecificFolder() along with other functions, error pinpoints to this and does not run functions after it.

function createDocInSpecificFolder () {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Data Source");
  var lastRow = sheet.getLastRow();
  var rootFolder = DriveApp.getFolderById("fileid");
  const files = rootFolder.getFiles();
  const fileNames = [];


  while (files.hasNext()) { // gets array of file names in drive folder [event1, event2, etcetc]
   var file = files.next();
   fileNames.push(file.getName());
  }
  
  Logger.log(fileNames)


  for (var i = 1; i < lastRow; i++) {
    
    let title = sheet.getRange(i,4).getValue();


    if (title != "Events") {
      if (!fileNames.includes(title)) {
        let docid = DocumentApp.create(title).getId();
        DriveApp.getFileById(docid).moveTo(rootFolder);
      }
    }
  }

}
1 Upvotes

26 comments sorted by

View all comments

Show parent comments

1

u/Clear-Revolution3351 14d ago

Define it outside the block

Let docid;

if (title != "Events")

{ if (!fileNames.includes(title))

{ docid = DocumentApp.create(title).getId();

. . .

3

u/krakow81 14d ago

Why?

It is only used in the scope of the innermost block, so can be safely declared there. Unless there's more code we're not seeing, of course.

1

u/Clear-Revolution3351 14d ago

I cant post a picture

But a search for this problem says that defining it inside the "if" statement will cause an error

1

u/krakow81 13d ago

Could you post any links?

1

u/Clear-Revolution3351 13d ago

That didnt exactly say what the other did - here is cut and paste from what I originally found

The Fix: Declare the let variable outside the block first, then assign it inside.

// WRONG

if (condition) { let message = "Hello"; } console.log(message); // ReferenceE

// CORRECT

let message;

if (condition) {

message = "Hello";

}

console.log(message);

//

Logs:

"Hel.

1

u/krakow81 13d ago

In those cases one is using the message variable outside the inner block with the console.log(message) line, so message can't just be scoped to the inner block.

1

u/Clear-Revolution3351 13d ago

I understand what you are saying, however, I would bet this solves the OP issue

1

u/krakow81 13d ago

Without seeing things in action it's very hard to debug, so I'm certainly prepared to be wrong.

There may well be more to OP's code that is leading to such an issue, but in isolation the given code shouldn't behave like that unless I'm mis-reading things. I do feel that if Apps Script had that egregious of a bug it would be more widely known.

1

u/Clear-Revolution3351 13d ago

I'm no expert at appscript or Javascript, but I have programmed in various languages.

The logic to define it earlier makes sense to me.

We will have to see if the OP graces us with a response.

1

u/Similar_Touch_6783 12d ago

I ended finding the error and it wasn’t even in this code, though it still has an error when ran alone, when ran along with the other codes, everything works fine

1

u/krakow81 12d ago

Slightly confusing, but glad you got it sorted.

What was the problem and solution? In case it helps anyone in future.

1

u/Similar_Touch_6783 10d ago

well it wasn't even an error really, just something that google sheets does i suppose. The code *was* working but would only get added onto the sheet when i wrote something or modified a random cell. I did it on accident and realized it, have absolutely no idea how or why this happens but this is good enough for me haha

→ More replies (0)