admin管理员组文章数量:1026253
Here is the problem & logic for the find & replace script I am using.
- Search Sheet for
to_replace
string.- If found, replace
to_replace
withreplace_with
. - If not found, replace
to_replace
withto_replace
// This is not needed, and causes problems (it replaces all formulas, and replaces it with a string).
- If found, replace
My Objective:
I would like the script to only replace cells that match to_replace
, and ignore every other cell.
My Rookie Solution:
Exclude specific columns in the foruma by eliminating column C from array using script from here. (only find & replace within Column B & D).
Here is the modified code I added in My Current Script...
const range = sheet.getRange('B2:D'+lastRow).getValues();
range.forEach(a => a.splice(1, 1)); //removes column C.
But I get the error: "TypeError: var data = range.getValues(); is not a function"
Question
Can you help me troubleshoot my rookie solution, or teach me a better way to solve this problem?
My current script
function findAndReplace(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow()
var lastColumn = sheet.getLastColumn()
// var range = sheet.getRange(1, 1, lastRow, lastColumn) //REMOVED - Searches all columns.
const range = sheet.getRange('B2:D'+lastRow).getValues(); //ADDED - Searches only B & D
range.forEach(a => a.splice(1, 1)); //ADDED - Searches only B & D
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var data = range.getValues();
var oldValue="";
var newValue="";
var cellsChanged = 0;
for (var r=0; r<data.length; r++) {
for (var i=0; i<data[r].length; i++) {
oldValue = data[r][i];
newValue = data[r][i].toString().replace(to_replace, replace_with);
if (oldValue!=newValue)
{
cellsChanged++;
data[r][i] = newValue;
}
}
}
range.setValues(data);
}
Here is the problem & logic for the find & replace script I am using.
- Search Sheet for
to_replace
string.- If found, replace
to_replace
withreplace_with
. - If not found, replace
to_replace
withto_replace
// This is not needed, and causes problems (it replaces all formulas, and replaces it with a string).
- If found, replace
My Objective:
I would like the script to only replace cells that match to_replace
, and ignore every other cell.
My Rookie Solution:
Exclude specific columns in the foruma by eliminating column C from array using script from here. (only find & replace within Column B & D).
Here is the modified code I added in My Current Script...
const range = sheet.getRange('B2:D'+lastRow).getValues();
range.forEach(a => a.splice(1, 1)); //removes column C.
But I get the error: "TypeError: var data = range.getValues(); is not a function"
Question
Can you help me troubleshoot my rookie solution, or teach me a better way to solve this problem?
My current script
function findAndReplace(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow()
var lastColumn = sheet.getLastColumn()
// var range = sheet.getRange(1, 1, lastRow, lastColumn) //REMOVED - Searches all columns.
const range = sheet.getRange('B2:D'+lastRow).getValues(); //ADDED - Searches only B & D
range.forEach(a => a.splice(1, 1)); //ADDED - Searches only B & D
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var data = range.getValues();
var oldValue="";
var newValue="";
var cellsChanged = 0;
for (var r=0; r<data.length; r++) {
for (var i=0; i<data[r].length; i++) {
oldValue = data[r][i];
newValue = data[r][i].toString().replace(to_replace, replace_with);
if (oldValue!=newValue)
{
cellsChanged++;
data[r][i] = newValue;
}
}
}
range.setValues(data);
}
Share
Improve this question
asked Jan 13, 2022 at 8:21
JamesReed68JamesReed68
4194 silver badges20 bronze badges
1 Answer
Reset to default 5From teach me a better way to solve this problem
, in your situation, I thought that when TextFinder is used, the process cost might be able to be reduced. When TextFinder is used for achieving your goal, it bees as follows.
Sample script:
function myFunction() {
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow();
var ranges = ['B2:B' + lastRow, 'D2:D' + lastRow];
sheet.getRangeList(ranges).getRanges().forEach(r =>
r.createTextFinder(to_replace).matchEntireCell(true).replaceAllWith(replace_with)
);
}
Note:
If you want to replace the part of cell value, please modify
r.createTextFinder(to_replace).matchEntireCell(true).replaceAllWith(replace_with)
tor.createTextFinder(to_replace).replaceAllWith(replace_with)
.As an additional modification, if your script is modified, how about the following modification?
function findAndReplace() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); var lastRow = sheet.getLastRow() var range = sheet.getRange('B2:D' + lastRow); var data = range.getValues(); var to_replace = "TextToFind"; var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string. for (var r = 0; r < data.length; r++) { for (var i = 0; i < data[r].length; i++) { var value = data[r][i].toString(); if (i != 1 && value.includes(to_replace)) { data[r][i] = data[r][i].replace(to_replace, replace_with); } } } range.setValues(data); }
References:
- createTextFinder(findText)
- Class TextFinder
Here is the problem & logic for the find & replace script I am using.
- Search Sheet for
to_replace
string.- If found, replace
to_replace
withreplace_with
. - If not found, replace
to_replace
withto_replace
// This is not needed, and causes problems (it replaces all formulas, and replaces it with a string).
- If found, replace
My Objective:
I would like the script to only replace cells that match to_replace
, and ignore every other cell.
My Rookie Solution:
Exclude specific columns in the foruma by eliminating column C from array using script from here. (only find & replace within Column B & D).
Here is the modified code I added in My Current Script...
const range = sheet.getRange('B2:D'+lastRow).getValues();
range.forEach(a => a.splice(1, 1)); //removes column C.
But I get the error: "TypeError: var data = range.getValues(); is not a function"
Question
Can you help me troubleshoot my rookie solution, or teach me a better way to solve this problem?
My current script
function findAndReplace(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow()
var lastColumn = sheet.getLastColumn()
// var range = sheet.getRange(1, 1, lastRow, lastColumn) //REMOVED - Searches all columns.
const range = sheet.getRange('B2:D'+lastRow).getValues(); //ADDED - Searches only B & D
range.forEach(a => a.splice(1, 1)); //ADDED - Searches only B & D
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var data = range.getValues();
var oldValue="";
var newValue="";
var cellsChanged = 0;
for (var r=0; r<data.length; r++) {
for (var i=0; i<data[r].length; i++) {
oldValue = data[r][i];
newValue = data[r][i].toString().replace(to_replace, replace_with);
if (oldValue!=newValue)
{
cellsChanged++;
data[r][i] = newValue;
}
}
}
range.setValues(data);
}
Here is the problem & logic for the find & replace script I am using.
- Search Sheet for
to_replace
string.- If found, replace
to_replace
withreplace_with
. - If not found, replace
to_replace
withto_replace
// This is not needed, and causes problems (it replaces all formulas, and replaces it with a string).
- If found, replace
My Objective:
I would like the script to only replace cells that match to_replace
, and ignore every other cell.
My Rookie Solution:
Exclude specific columns in the foruma by eliminating column C from array using script from here. (only find & replace within Column B & D).
Here is the modified code I added in My Current Script...
const range = sheet.getRange('B2:D'+lastRow).getValues();
range.forEach(a => a.splice(1, 1)); //removes column C.
But I get the error: "TypeError: var data = range.getValues(); is not a function"
Question
Can you help me troubleshoot my rookie solution, or teach me a better way to solve this problem?
My current script
function findAndReplace(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow()
var lastColumn = sheet.getLastColumn()
// var range = sheet.getRange(1, 1, lastRow, lastColumn) //REMOVED - Searches all columns.
const range = sheet.getRange('B2:D'+lastRow).getValues(); //ADDED - Searches only B & D
range.forEach(a => a.splice(1, 1)); //ADDED - Searches only B & D
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var data = range.getValues();
var oldValue="";
var newValue="";
var cellsChanged = 0;
for (var r=0; r<data.length; r++) {
for (var i=0; i<data[r].length; i++) {
oldValue = data[r][i];
newValue = data[r][i].toString().replace(to_replace, replace_with);
if (oldValue!=newValue)
{
cellsChanged++;
data[r][i] = newValue;
}
}
}
range.setValues(data);
}
Share
Improve this question
asked Jan 13, 2022 at 8:21
JamesReed68JamesReed68
4194 silver badges20 bronze badges
1 Answer
Reset to default 5From teach me a better way to solve this problem
, in your situation, I thought that when TextFinder is used, the process cost might be able to be reduced. When TextFinder is used for achieving your goal, it bees as follows.
Sample script:
function myFunction() {
var to_replace = "TextToFind";
var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow();
var ranges = ['B2:B' + lastRow, 'D2:D' + lastRow];
sheet.getRangeList(ranges).getRanges().forEach(r =>
r.createTextFinder(to_replace).matchEntireCell(true).replaceAllWith(replace_with)
);
}
Note:
If you want to replace the part of cell value, please modify
r.createTextFinder(to_replace).matchEntireCell(true).replaceAllWith(replace_with)
tor.createTextFinder(to_replace).replaceAllWith(replace_with)
.As an additional modification, if your script is modified, how about the following modification?
function findAndReplace() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); var lastRow = sheet.getLastRow() var range = sheet.getRange('B2:D' + lastRow); var data = range.getValues(); var to_replace = "TextToFind"; var replace_with = ""; //Leave blank to delete Text, or enter text in quotes to add string. for (var r = 0; r < data.length; r++) { for (var i = 0; i < data[r].length; i++) { var value = data[r][i].toString(); if (i != 1 && value.includes(to_replace)) { data[r][i] = data[r][i].replace(to_replace, replace_with); } } } range.setValues(data); }
References:
- createTextFinder(findText)
- Class TextFinder
本文标签: javascriptGoogle App Script Find amp Replace for specific columnsStack Overflow
版权声明:本文标题:javascript - Google App Script: Find & Replace for specific columns - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745624849a2159800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论