0
function sortColumn() {
  var spreadsheet = SpreadsheetApp.getActive();
  var range = spreadsheet.getRange('A3:G995');
  var value = range.getValue();
  range.setValue(value.toUpperCase());
};

Instead of turning the selected cell contents into uppercasing, the above script copies the content in A3 and pastes it to the rest of the cell. Any suggestion? Thanks!

Marios
  • 26,333
  • 8
  • 32
  • 52
AU CH
  • 23
  • 1

2 Answers2

1

There are two options.

You can either turn the value of a specific cell to uppercase (as it is suggested by the title of the question) or turn the value of every cell within a range of cells to uppercase (as it is suggested by the body of the question).

Solutions:

If you want to turn the content of A3 to uppercase:

function sortColumn() {
  var spreadsheet = SpreadsheetApp.getActive();
  var range = spreadsheet.getActiveSheet().getRange('A3');
  var value = range.getValue();
  range.setValue(value.toString().toUpperCase());
};

if you want to turn the content of each cell in A3:G995 to uppercase:

function sortColumn() {
  var spreadsheet = SpreadsheetApp.getActive();
  var range = spreadsheet.getActiveSheet().getRange('A3:G995');
  var value = range.getValues();
  var valueCap=value.map(val => val.map(v=>v.toString().toUpperCase()));
  range.setValues(valueCap);
};
Marios
  • 26,333
  • 8
  • 32
  • 52
1

Issue:

range.getValue() documentation says

Returns the value of the top-left cell in the range.

So, the rest of the cells are ignored.

Solution:

Use getValues() instead and recurse using map to change all values to uppercase and set back the modified array using setValues(arr)

Sample script:

const values = range.getValues();
const func = e => Array.isArray(e) ? e.map(func) : String(e).toUpperCase();
range.setValues(func(values))

Related:

What does the range method getValues() return and setValues() accept?

const values = /*Mock getValues*/ [['a','b'],['c','d']];
const func = e => Array.isArray(e) ? e.map(func) : String(e).toUpperCase();
const output = func(values);
console.info(output);
TheMaster
  • 45,448
  • 6
  • 62
  • 85