-1

I'm using the Discogs API to export my Wantlist to Google Sheets

Is there a way to add the HTML to make the release-url (resource_url) clickable in the output?

function logTheData (url){
  var sheet = SpreadsheetApp.getActiveSheet();
  var url ='https://api.discogs.com/users/bartvanbelle/wants?per_page=100';
  var response = UrlFetchApp.fetch(url); // get feed
  var json = response.getContentText(); //

  var data = JSON.parse(response);
  var counter = 100;
  for (var i = 0; i< counter; i++) {
  var stats = [];
    // var instance_id = data.wants[i].instance_id;
    
    //if (typeof data.wants[i].basic_information.formats[0].descriptions[0] !== "undefined"){
    //  var description = data.wants[i].basic_information.formats[0].descriptions[0]
    //  };
    
   // stats.push(instance_id);//works a
    stats.push(data.wants[i].basic_information.title); //works a
    stats.push(data.wants[i].basic_information.formats[0].name); 
           // stats.push(description); //stringify array?
  stats.push(String(data.wants[i].basic_information.formats[0].descriptions));  
  
    stats.push(data.wants[i].basic_information.labels[0].name);    //works c
    stats.push(data.wants[i].basic_information.labels[0].catno);        // work d
    stats.push(data.wants[i].basic_information.year); //l
   stats.push(data.wants[i].basic_information.artists[0].name); //works j  
   stats.push(data.wants[i].basic_information.id); //   m   
        stats.push(data.wants[i].basic_information.resource_url); //   m   
    
  Logger.log(stats);
    SpreadsheetApp.getActiveSpreadsheet().appendRow(stats);
  } 
  
   
  var pages = data.pagination.pages;
  for (var a = 1; a < pages; a++){
       var next = data.pagination.urls.next;
       var response = UrlFetchApp.fetch(next); // get feed
  var json = response.getContentText(); //

  var data = JSON.parse(response);
  var counter = 100;
  for (var i = 0; i< counter; i++) {
  var stats = [];
    
    stats.push(data.wants[i].basic_information.title); //works a
    stats.push(data.wants[i].basic_information.formats[0].name); 
  stats.push(String(data.wants[i].basic_information.formats[0].descriptions)); 
    stats.push(data.wants[i].basic_information.labels[0].name);    //works c
    stats.push(data.wants[i].basic_information.labels[0].catno);        // work d
    stats.push(data.wants[i].basic_information.year); //l
         // stats.push(description); //stringify array?
    stats.push(data.wants[i].basic_information.artists[0].name); //works j  
   stats.push(data.wants[i].basic_information.id); //   m  
      stats.push(data.wants[i].basic_information.resource_url); //   m 
  Logger.log(stats);
  
    SpreadsheetApp.getActiveSpreadsheet().appendRow(stats);
  } 
  } 
  
}

The resource URL is also formatted as http://api.discogs.com/.... Is there a way to convert that to http://www.discogs.com?

Jesse
  • 1,386
  • 3
  • 9
  • 23

2 Answers2

0

Maybe try something like:

stats.push('=HYPERLINK(data.wants[i].basic_information.resource_url,data.wants[i].basic_information.resource_url')

In general what works is to

  1. get the cell
  2. set the value
  3. set the formula (HYPERLINK(...))
  4. setShowHyperlink(true) on the cell.

Hope this helps.

You might aswell checkout this answer: https://stackoverflow.com/a/37488194/1698461

Lemonade
  • 503
  • 2
  • 8
0

I think you need to wrap data.wants[i].basic_information.resource_url into html tag

like this :

stats.push(
'<a href="' + data.wants[i].basic_information.resource_url + '">link</a>'
);

so full code ll be :

    function logTheData (url){
  var sheet = SpreadsheetApp.getActiveSheet();
  var url ='https://api.discogs.com/users/**********/wants?per_page=100';
  var response = UrlFetchApp.fetch(url); // get feed
  var json = response.getContentText(); //
  
  var data = JSON.parse(response);
  var counter = 100;
  for (var i = 0; i< counter; i++) {
    var stats = [];

    stats.push(data.wants[i].basic_information.title); //works a
    stats.push(data.wants[i].basic_information.formats[0].name); 
    stats.push(String(data.wants[i].basic_information.formats[0].descriptions));  
    
    stats.push(data.wants[i].basic_information.labels[0].name);    //works c
    stats.push(data.wants[i].basic_information.labels[0].catno);        // work d
    stats.push(data.wants[i].basic_information.year); //l
    stats.push(data.wants[i].basic_information.artists[0].name); //works j  
    stats.push(data.wants[i].basic_information.id); //   m   
    stats.push(data.wants[i].basic_information.resource_url); //   m   
    
    Logger.log(stats);
    SpreadsheetApp.getActiveSpreadsheet().appendRow(stats);
  } 
  
  
  var pages = data.pagination.pages;
  for (var a = 1; a < pages; a++){
    var next = data.pagination.urls.next;
    var response = UrlFetchApp.fetch(next); // get feed
    var json = response.getContentText(); //
    
    var data = JSON.parse(response);
    var counter = 100;
    for (var i = 0; i< counter; i++) {
      var stats = [];
      
      stats.push(data.wants[i].basic_information.title); 
      stats.push(data.wants[i].basic_information.formats[0].name); 
      stats.push(String(data.wants[i].basic_information.formats[0].descriptions)); 
      stats.push(data.wants[i].basic_information.labels[0].name);    
      stats.push(data.wants[i].basic_information.labels[0].catno);       
      stats.push(data.wants[i].basic_information.year); 
      stats.push(data.wants[i].basic_information.artists[0].name); 
      stats.push(data.wants[i].basic_information.id); 
      stats.push( '<a href="' + data.wants[i].basic_information.resource_url + '">link</a>');
      Logger.log(stats); 
      SpreadsheetApp.getActiveSpreadsheet().appendRow(stats);
      } 
    } 
    
  }
devseo
  • 1,182
  • 1
  • 13
  • 26