我想在电子表格中找到指定的文本并将其替换为其他单词。我试过这样的。
sheet = SpreadsheetApp.getActiveSheet()
sheet.replaceText ('ga: sessions','Sessions');
然后它说"Cannot find function replaceText in object Sheet."
答案 0 :(得分:5)
您可以通过读取工作表中的所有值(作为数组),循环数组,替换值,然后将整个数组写回工作表来实现此目的。
下面有一个基本的例子。如果工作表包含公式,或者如果要在给定单元格中多次替换文本,则可能需要修改此项。
请注意,在您读入和写出数据之间对工作表所做的任何更改都将丢失,并且可能会破坏此示例。
function testReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSheet()
replaceInSheet(sheet,'ga: sessions','Sessions');
}
function replaceInSheet(sheet, to_replace, replace_with) {
//get the current data range values as an array
var values = sheet.getDataRange().getValues();
//loop over the rows in the array
for(var row in values){
//use Array.map to execute a replace call on each of the cells in the row.
var replaced_values = values[row].map(function(original_value){
return original_value.replace(to_replace,replace_with);
});
//replace the original row values with the replaced values
values[row] = replaced_values;
}
//write the updated values to the sheet
sheet.getDataRange().setValues(values);
}
文档:
Array.map:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
String.replace:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Sheet.getDataRange:https://developers.google.com/apps-script/reference/spreadsheet/sheet#getDataRange()
range.GetValues:https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()
答案 1 :(得分:4)
function testReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSheet()
replaceInSheet(sheet,'values','test');
}
function replaceInSheet(sheet, to_replace, replace_with) {
//get the current data range values as an array
var values = sheet.getDataRange().getValues();
//loop over the rows in the array
for(var row in values){
//use Array.map to execute a replace call on each of the cells in the row.
var replaced_values = values[row].map(function(original_value){
return original_value.toString().replace(to_replace,replace_with);
});
//replace the original row values with the replaced values
values[row] = replaced_values;
}
//write the updated values to the sheet
sheet.getDataRange().setValues(values);
}
答案 2 :(得分:0)
getActiveSheet()函数返回一个Sheet对象,其功能记录在链接中。如果有这样的API会很棒,但是目前你需要删除和插入(行,列或特定范围的内容)才能进行那种替换。
答案 3 :(得分:0)
尝试这样的事情:
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange("A1:A1");
cell.setValue('ga: sessions','Sessions');
答案 4 :(得分:0)
这对我有用,而且很简单。
function changevalues() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('NameOfSheet');
var rangecells = sheet.getRange('DesiredRange');
rangecells.setValue('DesiredValue')
}
答案 5 :(得分:0)
所选答案不适用于包含日期单元格的数据。 实际上,它将这些单元格更改为String,这不是预期的行为。
我修改了代码以检查值是否为数字,并仅更改字符串值。
function testReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSheet()
replaceInSheet(sheet,'values','test');
}
function replaceInSheet(sheet, to_replace, replace_with) {
//get the current data range values as an array
var values = sheet.getDataRange().getValues();
//loop over the rows in the array
for(var row in values){
//use Array.map to execute a replace call on each of the cells in the row.
var replaced_values = values[row].map(function(original_value){
if (typeof original_value != 'string') {
return original_value;
} else {
return original_value.replace(to_replace,replace_with);
}
});
//replace the original row values with the replaced values
values[row] = replaced_values;
}
//write the updated values to the sheet
sheet.getDataRange().setValues(values);
}
祝你有美好的一天
答案 6 :(得分:0)
TextFinder于2019年4月发布:
function textFinder() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getDataRange();
// Creates a text finder for the range.
const textFinder = range.createTextFinder('findValue');
const allOccurrences = textFinder.replaceAllWith('replaceValue');
}