0

I am attempting to use Google Apps Script to download an excel file that is linked to on a CRM. That is, instead of each day having a person log in to the CRM, download new data as an excel file, and copy and paste the new data to our existing Google Sheet, I would like to have my script do so.

Is it possible to do this? I have been looking at UrlFetchApp, however I'm having trouble because there is no direct link to the file I need (only a link to generate and download the file).

user955789
  • 23
  • 2
  • What is the result of the link to generate and download the file? Does it give a second link (in which case you would need to parse the first page for it, then fetch again), or just return a spreadsheet (in which case you could just use that link)? – Richard Campbell Jun 27 '18 at 16:28
  • @RichardCampbell The link by itself (when opened in a new tab) just makes the page redirect to the homepage of the CRM, and when clicked regularly it just automatically downloads the Excel file, so no link there either. Unless you mean something else? – user955789 Jun 27 '18 at 16:34
  • That didn't help me. How many clicks is it from "the link to generate and download the file" to "the excel file is downloading?" – Richard Campbell Jun 27 '18 at 16:35
  • The reason I ask is that UrlFetchApp will, by default, follow redirects. – Richard Campbell Jun 27 '18 at 16:36
  • @RichardCampbell it's just one click – user955789 Jun 27 '18 at 16:37
  • So what is the actual problem you are having, what do you see on the responses from UrlFetchApp, etc.? – Richard Campbell Jun 27 '18 at 16:38
  • @RichardCampbell currently I am getting the message "Request failed for [site] returned code 302 (line 2, file "ImportExcel")" – user955789 Jun 27 '18 at 16:48
  • Code 302 is a redirect. Can you confirm that the followRedirects parameter is being set to true as a parameter on the http request? – Richard Campbell Jun 27 '18 at 18:31
  • @RichardCampbell It is, yes – user955789 Jun 27 '18 at 18:35
  • You need to look at all aspects of your HTTP response object - I suspect that there is ultimately an authentication problem and/or cookies not getting passed from redirect to redirect. If there is a login problem, you may need to follow the approach given here: https://stackoverflow.com/questions/28794290/why-is-my-urlfetchapp-function-failing-to-successfully-login/29000773#29000773 – Richard Campbell Jun 27 '18 at 18:42
  • @RichardCampbell I will give that a shot. Thank you so much for all of your help! – user955789 Jun 27 '18 at 19:16

0 Answers0