Question

Can anybody show me how to get XBRL data into google spreadsheets using google script?

http://www.fossil.com/attachments/en_US/financials/2013/fosl-20130330.xml

here is the link.

function XBRLexplore() {
  var target = "http://www.fossil.com/attachments/en_US/financials/2013/fosl-20130330.xml";
  var pageTxt = UrlFetchApp.fetch(target).getContentText();
  var pageDoc = Xml.parse(pageTxt,true);

//var temp=getDivById( pageDoc.getElement(), 'Tag204' );
//var temp=getElementById('Tag204')
}

I can't seem to get to "tag204" the first element. not sure how to use the get by id functions correctly.

Was it helpful?

Solution

Look at previous questions about parsing XML documents, since XBRL is an application of XML using a standardized taxonomy of business terms. (Remember - HTML documents are XML with tag, or element, types specific to web page design.)

For example, this answer shows some very basic navigation of xml elements. And you should recall this one that explained it in greater detail.

The same techniques apply, but with element types such as AntidilutiveSecuritiesExcludedFromComputationOfEarningsPerShareAmount (The element containing id="Tag204" is one of these.)

<us-gaap:AntidilutiveSecuritiesExcludedFromComputationOfEarningsPerShareAmount id="Tag204" decimals="0" contextRef="D2013Q1" unitRef="Shares">201000</us-gaap:AntidilutiveSecuritiesExcludedFromComputationOfEarningsPerShareAmount>

The XML element name consists of two parts, a required name prefixed by an optional namespace. In this case, the namespace is us-gaap, which tells us the vocabulary source.

Here's how you could get that one element, using the getElementByVal() utility introduced in this answer, and write it to a spreadsheet:

function xbrl() {
  var target = "http://www.fossil.com/attachments/en_US/financials/2013/fosl-20130330.xml";
  var pageTxt = UrlFetchApp.fetch(target).getContentText();
  var xbrl = Xml.parse(pageTxt,true).getElement();

  var Tag204 =
      getElementByVal( xbrl,
                      'AntidilutiveSecuritiesExcludedFromComputationOfEarningsPerShareAmount',
                      'id',
                      'Tag204' );

  var data = [];
  var row = [];
  var name = Tag204.getName().getLocalName();
  row.push(name);
  var attributes = Tag204.getAttributes();
  for (var j=0; j<attributes.length; j++) {
    row.push(attributes[j].toXmlString())
  }
  data.push(row);

  var sheetId = '---Sheet-Id---';
  var sheet = SpreadsheetApp.openById(sheetId).getSheetByName("XBRL");
  sheet.getRange(1,1,data.length,data[0].length).setValues(data);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top