0

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;
}
9
  • Re: =Logger of course it will - take a look at what log method call returns Commented Jun 15, 2020 at 3:28
  • Also: what are addUrl and addUrl2 used for - they seem to have not being referenced by anything else? Please, post a screenshot of what the output looks like (don't share the spreadsheet, please) And it would help if you added the code for getLatestFile (in the question) Commented Jun 15, 2020 at 3:35
  • 1
    Added the picture Commented Jun 15, 2020 at 3:51
  • 1
    Ah, here we go - function names have to unique. The best case is that getLatestFile gets called with the second code, the worst - the result is indeterministic Commented Jun 15, 2020 at 6:03
  • 1
    No problem, but you should definitely get rid of the second one - they are the same, just reuse the first one - I will add an answer shortly Commented Jun 15, 2020 at 6:10

1 Answer 1

2

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>

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

2 Comments

Thanks a lot ! Saved me from a lot of troubles, I almost gave up, thinking it was impossible.
My pleasure! When you think that your code should definitely work, but it does not - always check if the naming is correct, execution reaches the desired point, etc, etc. Btw, you can use the built-in debugger to test what exactly happens at each step

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.