Help

Re: Code to retrieve url in Attachments field

4038 0
cancel
Showing results for 
Search instead for 
Did you mean: 
DanielAssayag
6 - Interface Innovator
6 - Interface Innovator

Hi,
Here is my code using record.get([“Attachments”]); :

function showListe() {
  base("Works")
    .select({ view: "Grid view" })
    .eachPage(function page(records) {
      records.forEach(function(record) {
        var li = document.createElement("li");  
        li.textContent = record.get(["Attachments"]);
        document.getElementById("root").appendChild(li);
      });
    });
}

which gets me :

  • [object Object]
  • [object Object]

If i do record.get([“Attachments”][“url”);
i get nothing…

Help!

13 Replies 13
Matthew_Katz
4 - Data Explorer
4 - Data Explorer

I worked through this on my own, here is my hobbyist solution… good is good enough, right?

// get the entire attachments data… everything
var urldata = [record.get(‘Attachments’)];

// stingify the data into one messey long piece of text.
var urldata2 = JSON.stringify(urldata);

// split the string into an array using an apostrophe (you need \" in the quotes)
var urldata3 = urldata2.split("\"");

// publish the URL so that I can make my link reference.
Format.href = urldata3[7];

EASY as PIE - Works in any Setup… good luck fellow coders :slightly_smiling_face:

Juan_Pablo
4 - Data Explorer
4 - Data Explorer

@Bill.French I am working in Google Scripts trying to send all of my data to a google sheet but the 100 records limit is killing me. I see you put here a method to extract the first hundred, do you have the iterative function to go on?

Sure.

//
// get airtable records
//
function atGetTable_(baseID, tableName, apiKey)
{

  // set the key if not passed in the argument
  apiKey = (apiKey == undefined) ? airtableAPIKey : apiKey;

  // create the urlfetch options object
  const options = {
    method: 'GET',
    headers: {
      'Authorization' : 'Bearer ' + apiKey,
      'Content-type': 'application/json'
    },
    muteHttpExceptions : true
  };
  
  // setup the page loop
  var offSet = true;
  var currentPage = 0;
  var aRecords = [];
  var offsetParameter = "";

  // iterate across the pages
  while (offSet)
  {

    // log the page number to the console
    Logger.log("Reading page " + currentPage);

    // call the airtable api
    var response = UrlFetchApp.fetch(airtableAPIEndpoint + baseID + "/" + encodeURIComponent(tableName) + "?pageSize=100" + offsetParameter, options).getContentText();
    
    // concat the array for of records for the current page 
    aRecords = aRecords.concat(JSON.parse(response).records);
    var offSet = JSON.parse(response).offset;

    // is there more data?
    if (offSet)
    {
      offsetParameter = "&offset=" + encodeURIComponent(offSet);
    } else {
      break;
    }

    // increment the page number
    currentPage += 1;
  }
  
  var oRecords = {
    "records" : aRecords
  }
  
  return(JSON.stringify(oRecords));
  
}
Jaydeep_Gajera
4 - Data Explorer
4 - Data Explorer

This seems hacky but worked for me.

${JSON.parse(JSON.stringify(record.get([‘Image’][0])))[0][‘url’]}