r/GoogleAppsScript • • Jul 15 '26

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

27 comments sorted by

View all comments

1

u/[deleted] Jul 15 '26 edited Jul 15 '26

[removed] — view removed comment

3

u/krakow81 Jul 15 '26

It's defined on this line:

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

1

u/[deleted] Jul 15 '26

[removed] — view removed comment

3

u/krakow81 Jul 15 '26

let and const are variable declarations too (and often preferred/recommended over var).

1

u/[deleted] Jul 15 '26

[removed] — view removed comment

3

u/krakow81 Jul 15 '26

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/[deleted] Jul 15 '26

[removed] — view removed comment

1

u/krakow81 Jul 15 '26

Could you post any links?

1

u/[deleted] Jul 15 '26

[removed] — view removed comment

1

u/krakow81 Jul 15 '26

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/[deleted] Jul 15 '26

[removed] — view removed comment

1

u/krakow81 Jul 15 '26

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.

→ More replies

1

u/WicketTheQuerent Jul 17 '26

In JavaScript/Google Apps Script, there is no need to define the variable type before assigning a value.

Also, since Google Apps Script added the V8 runtime, let and const are recommended over var.