-1

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.

jason
  • 3,811
  • 18
  • 92
  • 147
  • What have you tried ? It would help if you post some code of your attempt and seek help on a specific problem you are facing rather than ask "give me the code" – Srik Jul 21 '13 at 05:48
  • ha. I would but I don't know how. that's why I'm asking. I just need some starting code. Like what this guy provided here. You can see from my previous posts that I do include what code i have. http://stackoverflow.com/questions/14805527/grabbing-table-from-html-using-google-script – jason Jul 21 '13 at 06:51
  • https://developers.google.com/apps-script/reference/xml-service/ provides a start for you – DavidF Jul 21 '13 at 18:19
  • @DavidF. ok thanks. I just want the first element. It has a "Tag204" for ID. How do i grab that? i've tried the code above. – jason Jul 22 '13 at 05:36

1 Answers1

1

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);
}
Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • If I wanted to retrieve all of the data into a google spreadsheet, what's the best way to do it? I'm learning about SOAP, should I use that? or is javascript good enough? – jason Aug 03 '13 at 01:06
  • 1
    SOAP is a protocol for exchanging objects (using XML), Javascript is a programming language. You can work with SOAP using Javascript, certainly. – Mogsdad Aug 03 '13 at 03:31
  • can you show me a few lines of code to get all the data? I can do the spreadsheet part. I think I can handle that now. I just need a way to most efficiently get the data into a format that I can place into the spreadsheet. – jason Aug 03 '13 at 06:00
  • What I'm essentially trying to do is drop all of the "us-gaap" related elements in one sheet, while dropping all of the "fosl:" elements in another sheet. If you show me how to do one, I can do the other. – jason Aug 03 '13 at 06:05
  • I've flatten the structure. But I'm having trouble with the namespace and local name. quick pointer on how to do it would be appreciated. http://stackoverflow.com/questions/18040867/google-script-xml-get-parent – jason Aug 04 '13 at 11:46