0

Is there any possibility to configure a specific range of cells in Google Sheets to:

  1. If they are filled by text, the text is aligned to the center?
  2. If they are filled by numbers, numbers are aligned to the right?

The default setting is: numbers to the right, text to the left, but can I change it in any way?

player0
  • 124,011
  • 12
  • 67
  • 124
  • if possible then only via script – player0 Nov 21 '19 at 20:27
  • It is possible, I recommend you to take a look at the [Range Class](https://developers.google.com/apps-script/reference/spreadsheet/range), the [onEdit()](https://developers.google.com/apps-script/guides/triggers#onedite) trigger, the [setHorizontalAlignment method](https://developers.google.com/apps-script/reference/spreadsheet/range#sethorizontalalignmentalignment) and [this](https://stackoverflow.com/questions/35252684/if-var-isnumber-for-script) answer regarding the numeric values. – Jescanellas Nov 22 '19 at 08:18

1 Answers1

-1

Here you go -

function onEdit(e) {
  var range = e.range;
  var value = range.getValue();
  if (!isNaN(value)) {
    range.setHorizontalAlignment("right");
  } else {
    range.setHorizontalAlignment("center");
  }
}

Note that this script checks if the value is either a number or not a number and based on that, sets up the alignment.

Hope this helps!

Sourabh Choraria
  • 2,255
  • 25
  • 64