url fetch app to specified google spreadsheet

519 views
Skip to first unread message

Michal

unread,
Oct 16, 2020, 5:21:24 AM10/16/20
to Google Ads Scripts Forum
Hi,
I need help with simple script.
 I'm trying to fetch data into specified google sheet but by far it is only sent data to looger log.

Script below:

function main() {

  fetchapp();
  spreadsheetHelper();
  
}

  function fetchapp() {
var response = UrlFetchApp.fetch("my http");
Logger.log(response.getContentText());

}

function spreadsheetHelper() {
  
  var SPREADSHEET_URL = 'my specified spreadsheet url';
  var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  var sheet = ss.getSheets();
  
  
}

Sigurd Fabrin

unread,
Oct 16, 2020, 7:01:31 AM10/16/20
to Google Ads Scripts Forum
Hi Michal,

Normally you would run this on many URLs from an array - but if you do so and export everything, your spreadsheet will get really bloated.
I'd recommend you to extract the valuable data in the script and only export those bits of of information.

Here's a simple script that will export an array of URLs and their http response codes:


var settings = {
  
  sheet: 'SheetName',
  
}

function main() {  
  var data = [];
  var params = {followRedirects:true,muteHttpExceptions:true}; // see https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#getRequest(String,Object)
    for (i=0;i<urls.length;i++) {
      try { // I recommend using try/catch with UrlFetchApp as it is rather error prone
        var response = UrlFetchApp.fetch(urls[i],params)
        var responseCode = response.getResponseCode(); 
          Logger.log('Http response code: '+responseCode + ' for this URL: '+urls[i])
            data.push([responseCode,urls[i]]);
        }
      catch(e) {
        Logger.log('Got this error: '+e) // log errors
      }
    }
  data.unshift(['http response','url']); // prepend headlines
   var ss = SpreadsheetApp.openByUrl(settings.ssUrl);
   var sheet = ss.getSheetByName(settings.sheet);
    sheet.getDataRange().clearContent(); // delete old data from sheet
      var range = sheet.getRange(1,1,data.length,data[0].length); // start in A1
        range.setValues(data)
          Logger.log('*** Data exported to this spreadsheet: '+settings.ssUrl)
}

Michal

unread,
Oct 16, 2020, 10:06:05 AM10/16/20
to Google Ads Scripts Forum
thanks for helping me, but it is not what I really need. 
To be more precise - I need to fetch data from specidfied url by UrlFetchApp  to specified by me google sheet.
For example i have got  https://xyz.csv and i want to fetch it to specified spredasheet f.e. https://docs.google.com/spreadsheets/xyz 
I want to have all data or to be able to specify some usefull headlines for me.

Sigurd Fabrin

unread,
Oct 16, 2020, 10:55:35 AM10/16/20
to Google Ads Scripts Forum
So, just switch

.getResponseCode();
with
.getContentText()

However, you probably don't wish to upload all the stuff it fetches in it's entirety - at least not if you have 5K URLs or so.

Google Ads Scripts Forum Advisor

unread,
Oct 19, 2020, 3:10:52 AM10/19/20
to adwords-scripts+apn2wqevinbbivog...@googlegroups.com, adwords...@googlegroups.com
Hi Sigurd,

Thank you for sharing you knowledge to the community.

@Michal,

Feel free to get back to us if you have any other concern. We'll be happy to assist you.

Regards,
Google Logo
Teejay Wennie Pimentel
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q269m2x:ref

Michal

unread,
Oct 19, 2020, 3:38:26 AM10/19/20
to Google Ads Scripts Forum
unfortunately there is no data form given url in spredsheet. The only data are: "http response" in A1 and "url" in B1 
I need some further assist, please

Google Ads Scripts Forum Advisor

unread,
Oct 19, 2020, 5:59:04 AM10/19/20
to adwords...@googlegroups.com
Hi there,

Thank you for getting back to us. 

For us to check your concern further, could you please provide the following details via Reply privately to author option?
  • CID
  • Script name in question
  • Shareable link of the spreadsheet being used in the script
Reply all
Reply to author
Forward
0 new messages