0

I am trying to set up a Google sheet that serves as a register for technical documents that have been created (PDFs) and also gives an indication if the document has been loaded to the web server.

The intent is for the users to add in the document name in one column. Then in another column parse that file name with other text to build the hyperlink to where the file should be located once it is uploaded.

Then in another column I would like to have some sort of function that can indicate to the user that the hyperlink does not return a 404 error. This is the part I am lost on. I have done a little scripting before but have no idea where to start with this. I have searched in this forum and found some posts about UrlFetchApp.fetch which looks like it might get the information I am looking for. However I am unsure how to build a custom function with it that would return a message in the spreadsheet to indicate the link it is testing is broken.

Ilya Kern
  • 89
  • 1
  • 4
  • 14
  • Can you add any details like: code used, error problem encountered? [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) Show the community what you have tried. – Mr.Rebot Sep 30 '17 at 18:27
  • Thanks for the response. I have added a bit more detail to the post. – Ilya Kern Oct 01 '17 at 00:51

1 Answers1

1

Here's your function that you would want

function chkUrl(url) { 
 var result = UrlFetchApp.fetch(url, { 
 muteHttpExceptions: true }); 
 return result.getResponseCode(); 
}

And I've tested it with this spreadsheet and currently it returns response codes back, but you can make it return a proper text that works for you like..

return result.getResponseCode()==404; 

OR

return result.getResponseCode()==404 ? "broken" : "working"; 

Output
enter image description here

kaza
  • 2,317
  • 1
  • 16
  • 25