Google Script: How do I get my output in the console pasted in google sheets?

Viewed 65

This is my current code:


function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
     sheet.appendRow(["Remove_Later", "Name", "Client", "Name of project", "Deliverables"]); //writes the headers
  var topFolder = DriveApp.getFolderById("ThisIsTheFolderId");
  var topPath = "/";
  iterateSubFolders(topFolder, topPath);
}

function iterateSubFolders(callingFolder, callingPath) {

  var callingFolderName = callingFolder.getName();
  var callingFolderFullPath = callingPath + callingFolderName + "/";
  Logger.log(callingFolderFullPath);

  var childSubFolders = callingFolder.getFolders();
  while (childSubFolders.hasNext()) {
      var nextSubFolder = childSubFolders.next();
      iterateSubFolders(nextSubFolder, callingFolderFullPath);
  }
}

I have not been able to get the console output in a google sheet which i would like. I have no clue and have been struggling for hours now. Help is highly appreciated! Thank you in advance :)

1 Answers

You can return your console output to myFunction and pass it to sheet.appendRow

Sample:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var topFolder = DriveApp.getFolderById("ThisIsTheFolderId");
  var topPath = "/";
  iterateSubFolders(topFolder, topPath);
  sheet.appendRow(["Remove_Later", "Name", "Client", "Name of project", "Deliverables", callingFolderFullPath]); //writes the headers
}

function iterateSubFolders(callingFolder, callingPath) {

  var callingFolderName = callingFolder.getName();
  var callingFolderFullPath = callingPath + callingFolderName + "/";
  sheet.appendRow([callingFolderFullPath]);
  var childSubFolders = callingFolder.getFolders();
  while (childSubFolders.hasNext()) {
      var nextSubFolder = childSubFolders.next();
      iterateSubFolders(nextSubFolder, callingFolderFullPath);
  }
}
Related