Answer the question
In order to leave comments, you need to log in
How to change the value in an array?
Good afternoon.
Can you please tell me how can I change the value in a number of cells at once?
Through getValues() I get an array of data. And through the loop, referring to each cell, through replace () I can change by overwriting the data in the cell.
And if there is a way to change the text in the cells without accessing them through a loop? In the google script documentation, I found (as it seems to me) a suitable replaceAllWith(replaceText) method, through which, as I understand it, you can immediately change the desired value in all cells. But I didn’t understand how to use this method (not how it doesn’t work out)
I would be grateful if you answer my question.
thank.
Answer the question
In order to leave comments, you need to log in
There, in fact, there will also be a cycle. Here is the code that does what you need
function test_replaceInRange(){
let rangeAddress = "A:C";
let find = "1";
let replaceWith = "22";
replaceInRange(rangeAddress, find, replaceWith);
};
/**
* Заменяет find на replaceWith в диапазоне rangeAddress
* Telegram - @ProgrammerForever
*
* @param {string} rangeAddress Адрес диапазона
* @param {string} find Что менять
* @param {string} replaceWith На что менять
* @return Не возвращает значений
*/
function replaceInRange(rangeAddress, find, replaceWith){
find = find===undefined?"":(find+"");
replaceWith = replaceWith===undefined?"":(replaceWith+"");
let as = SpreadsheetApp.getActiveSpreadsheet();
let range = as.getRange(rangeAddress);
let values = range.getValues();
let replacedValues = values.map(row=>row.map(el=>(el+"").split(find).join(replaceWith)));
range.setValues(replacedValues);
range.activate();
as.toast("Done!");
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question