简体   繁体   中英

How to convert time format to a number format (Google Sheets/Data Studio)

I want to convert the numbers in ColA to normal numbers, like the ones I have in ColB. The one in ColA currently are in this format: 00:56:00.000. I initially started out with a time length, that I then converted to seconds (what's currently in ColA). I need to convert them back into a single number to import into Data Studio so it can read it as time. If I try to change the formatting to a number it comes up as a decimal, how can I change the format to a number? Or can I do it in Data Studio?

https://docs.google.com/spreadsheets/d/1xaAfc6hcFJJ1JsPfrx9Ly3usfljuzELT80UsgSxcNM8/edit#gid=0

Try this on col B3 on Google Sheets while A3:A is converted into number format (in decimals):

=Arrayformula((A3:A*3600)*24)

Output:

在此处输入图像描述

Explanation:

This is just an observation, but it seems that Google Data Studio strictly follows the formatting of the cell values. If you want to read the seconds as integers, a few arithmetic operations was performed to convert the number formatting of duration as such presented above.

Apps Script (use this if removing helper column B):

function myFunction() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var data = ss.getRange('A3:A').getValues();

  Logger.log(data);

  var second = 3600;
  var hour = 24;
  var output = data.map(x => [x[0] * second * hour]);
  ss.getActiveSheet().getRange(3, 1, output.length, output[0].length).setValues(output);
}

Output:

在此处输入图像描述

Change format to seconds only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM