Search code examples
google-apps-scriptgoogle-sheetsstackdriver

Using Logger.Log to log different value


I was wondering: is it even possible to use Logger.Log in Google Apps Script to log different string to be posted to a spreadsheet?

I have the following code:

var ss = SpreadsheetApp.openByUrl("spreadsheet url");
var sheet = ss.getSheetByName("spreadsheet sheet");
var DocNumber = e.parameter.DocNumber;
  var folderId = "Folder ID 1";
  var lastFileUrl = getLatestFile(folderId); // just a function that retrieves url of latest file in the folder

  Logger.log(lastFileUrl);

  var addUrl = sheet.getRange(1,2,sheet.getLastRow(),1);
  var fileURL = "https://drive.google.com/uc?export=view&id="+lastFileUrl;


  var folderId2 = "Folder ID 2";
  var lastFileUrl2 = getLatestFile(folderId2); // same as above

  Logger.log(lastFileUrl2);

  var addUrl2 = sheet.getRange(1,3,sheet.getLastRow(),1);
  var fileURL2 = "https://drive.google.com/uc?export=view&id="+lastFileUrl2;


sheet.appendRow([DocNumber,fileURL,fileURL2]);

}

When this get posted to the spreadsheet, it only posts the second url (fileURL2) - I assume because the last value in the log is this. But I was hoping to post both URL into the spreadsheet.

I tried setting it as a var first as well:

var URL2 = Logger.log(lastFileURL2);

but then the posted value will be https://drive.google.com/uc?export=view&id=Logger

I also tried using appendRow before the second URL logging but it still only takes the second url and disregard the first url.

Therefore, I was curios whether this is even possible at all?

And if not, what's the best way to achieve this without using Logger.log?

Spreadsheet output:

sheet structure

URL1 and URL2 is the URL from Google Drive folder. Also, forgot to mention, I'm using the script as a Web App, used by an android app. Posting files into the Drive folder is okay, the only problem is fetching the links of the files in different folders.

These are the codes I used to get the latest file url from my folders:

function getLatestFile(folderId) {
  var files = DriveApp.getFolderById("Folder_1_ID").getFiles();
  var fileObj = [];
  while (files.hasNext()) {
    var file = files.next();
    fileObj.push({id: file.getId(), date: file.getDateCreated()});
  }
  fileObj.sort(function(a, b) {return new Date(b.date) - new Date(a.date)});
  return fileObj[0].id;
}


function getLatestFile(folderId2) {
  var files2 = DriveApp.getFolderById("Folder_2_ID").getFiles();
  var fileObj2 = [];
  while (files2.hasNext()) {
    var file2 = files2.next();
    fileObj2.push({id: file2.getId(), date: file2.getDateCreated()});
  }
  fileObj2.sort(function(a, b) {return new Date(b.date) - new Date(a.date)});
  return fileObj2[0].id;
}

Solution

  • Problem

    Having two functions declared under the same name

    Solution

    Step by step:

    1. Remove one of the functions (they are identical in terms in usage)
    2. Make the remaining one use the parameter passed in it:
    function getLatestFile(folderId) {
      var files = DriveApp.getFolderById(folderId).getFiles();
      var fileObj = [];
      while (files.hasNext()) {
        var file = files.next();
        fileObj.push({id: file.getId(), date: file.getDateCreated()});
      }
      fileObj.sort(function(a, b) {return new Date(b.date) - new Date(a.date)});
      return fileObj[0].id;
    }
    
    1. Change Logger to console - as of recently, all logs are sent to Stackdriver service, and thus there is no benefit in using Logger (besides by using console you make script more portable).

    Commentary

    What happens when you declare two or more functions under same name? Normally, the last one declared gets executed (basically, second declaration overwrites the first):

    function clone(original) {
      return `I am the clone of ${original}`;
    }
    
    function clone(cloned) {
      return `I am a clone of ${cloned}'s clone`;
    }
    
    const elem = document.querySelector("#cloned");
    elem.textContent = clone("Gary");
    <h2 id="cloned"></h2>