0

Is it possible to format a GS cell via a script? Datas from my script are duration, but when I copy them in GS, when the value is less than 24:00 GS considers them as date, and when the value is more than 24:00 GS considers them as duration.

I can manually format the cell with duration in GS, but there is a loooot of cells, so I'd like to do it with a script.

Is it possible?

Thanks,

Adblayrog

1
  • Check the Range Class there are several ways to change format including setNumberFormat Commented Jul 13, 2021 at 14:32

1 Answer 1

1

You can use setNumberFormat() function.

var ss = SpreadsheetApp.getActiveSheet();
ss.getRange('A1:A10').setNumberFormat('[hh]:mm:ss');

Reference

But if your durations are in a column or a row, you can select them all and format as duration without any need to a script.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response. I tried with .setNumberformat(), but the problem is still there : if the duration is less than 24:00, GS identifies the value like a date instead of a duration. I know I can manually format a range directly with GS, but each day I have ten new files created, and I do not want to do it manually. So I'd like a solution with a script to format a range like a duration.
Sorry, it was my mistake. I wrote without the square brackets... But now, with the brackets, I have a new problem. Ex : for 2 hours and 10 minutes, it is written "02:130:7800". So for each unit, it converts the total duration. But thanks, your response was good. I will look for the source of my new problem.
Just like you said, it converts total duration for each unit freakishly. When I tried with "[hh]:ss.mm" it worked. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.