0

I am having trouble to convert a string 'u12345678910' to an integer without scientific notation. So basically, it literally needs to be exactly 12345678910.

How I have approached it:
1. slice the 'u' to get the number i want (after slice its still a string) OR use .replace()
2. tried parseInt(), Number(), both give me a number in scientific notation.
3. parseFloat() & then parseInt(), don't know why but read somewhere that this might work, it didn't.

Why do i need this exact number?
Since it is a chartId of an EmbeddedChart & Google Slides API is giving me the error that the ID needs to be of TYPE_INT32.

Hopefully someone has a solution to this, since its the only thing blocking my project at the moment :(

Please find below some sample code of how to reproduce.

*I am working in Google Apps Script, Chart is in Spreadsheet & I am using the Google Slides API library of Spencer Easton

var ss = SpreadsheetApp.getActiveSpreadsheet(),
  allSheets = ss.getSheets();
var chart = allSheets[0].getCharts()[0],
    chartId = chart.getId(); // this returns 'u12345678910'

var chartStringNumber = chartId.slice(1); // returns 12345678910 (string)
var chartIdNumber = Number(chartStringNumber); // Here I want the result to be typeof INT & 12345678910, but i keep getting a number incl scientific notation. I have also tried parseInt().
PdB
  • 50
  • 1
  • 7
  • The notion is probably just formatting when the number is printed. If you include the parts of your code thats [showing the problem](http://stackoverflow.com/help/mcve), its more likely someone can help you. – rckrd Feb 26 '17 at 17:39
  • You don't give any information about the context you are working in. Spreadsheet ? Webapp? – Serge insas Feb 26 '17 at 17:48
  • Thank you for your comments, ur completely right. I have added sample code to illustrate my issue. I am working with an embedded chart in Spreadsheet. – PdB Feb 26 '17 at 18:25
  • Try the solution in a related SO [post](http://stackoverflow.com/a/40685642/5995040) by using unary operator '+' or parseInt(number,10) or Number() to convert a string to number if that solve your issue. – Mr.Rebot Feb 27 '17 at 09:22

1 Answers1

1

The chartID returned by the built-in SpreadsheetApp service is a string value that can be overridden using setId(), and starts as an arbitrary value - so it's not the id you want.

To use the Slides API you need the 'real' chart ID which is available from the Advanced Spreadsheet Service, or the directly from the Sheets API.

You can use this code to enumerate the sheets and their charts & real ids using the Advanced Service.

var ch = Sheets.Spreadsheets.get(ss.getId(), {
       "fields": "sheets(charts/chartId,properties(sheetId,title))"
});

var sheets = ch.sheets.map (function (d) {
  return {
    id:d.properties.sheetId,
    name:d.properties.title,
    charts:d.charts
  }
});
bruce
  • 1,408
  • 11
  • 33