Google apps script Google应用程序脚本:仅在特定列中查找和替换

Google apps script Google应用程序脚本:仅在特定列中查找和替换,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在使用下面的代码在谷歌电子表格中运行搜索和替换。这是基于我在上面找到的答案 我的问题是我不能让它在电子表格中只运行一个特定的列 我想搜索D列中的ABC,然后将ABC替换为D列中的Subject:ABC 我尝试过定义var值=行[3];但这会引发一个错误,无法找到方法setValuesstring。第27行,文件替换。我尝试过各种类似的修改,但都不起作用 如何仅在特定列中搜索和替换 function run() { runReplaceInSheet(); replaceInSheet();

我正在使用下面的代码在谷歌电子表格中运行搜索和替换。这是基于我在上面找到的答案

我的问题是我不能让它在电子表格中只运行一个特定的列

我想搜索D列中的ABC,然后将ABC替换为D列中的Subject:ABC

我尝试过定义var值=行[3];但这会引发一个错误,无法找到方法setValuesstring。第27行,文件替换。我尝试过各种类似的修改,但都不起作用

如何仅在特定列中搜索和替换

function run() {
runReplaceInSheet();
replaceInSheet();
}

function runReplaceInSheet() {

var spreadsheet = SpreadsheetApp.openById("ID"); 
var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]); 
var startRow = 2; // First row of data to process
var numRows = 2; //  number of rows to process
// Fetch the range of cells 
var dataRange = sheet.getRange(startRow, 1, numRows, 22) // Numbers of rows to process
// Fetch values for each row in the Range
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var values = sheet.getDataRange().getValues();  

    // Replace Names

    replaceInSheet(values, 'ABC', 'Subject: ABC');

    //write the updated values to the sheet, again less call;less overhead
    sheet.getDataRange().setValues(values);        

}
}

function replaceInSheet(values, to_replace, replace_with) {

//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;


}
}
编辑

这是可行的,但它添加了两次主题,即主题:主题:ABC

function runReplaceInSheet() {

var spreadsheet = SpreadsheetApp.openById("ID"); // UPDATE ID
var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);     // UPDATE number in square brackets
var range = sheet.getRange("D2:D4");
//  get the current data range values as an array
//  Lesser calls to access the sheet, lower overhead 
var startRow = 2; // First row of data to process
var numRows = 2; // UPDATE number of rows to process
// Fetch the range of cells 
var dataRange = sheet.getRange(startRow, 4, numRows, 1) // Numbers of rows to process
    // Fetch values for each row in the Range
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var v = row[3]; // edit: don't need this
    var values = range.getValues();  

    // Replace Names

    replaceInSheet(values, 'ABC', 'Subject: ABC');

    //write the updated values to the sheet, again less call;less overhead
    range.setValues(values);        

}
}

这里有一些适合我的代码

function findingReplacing()
{
  var s = SpreadsheetApp.getUi().prompt("Search String", "Please enter desired search string", SpreadsheetApp.getUi().ButtonSet.OK).getResponseText();
  var rng = SpreadsheetApp.getActiveSheet().getRange('D:D');
  var rngA = rng.getValues();
  for(var i=1;i<rngA.length;i++)
  {
    if(rngA[i][0]==s)
    {
      rngA[i][0]='Subject: ' + s;
    }
  }
  rng.setValues(rngA);
}
以下是我的数据


我在D列中搜索了David Stew,它将该字符串替换为Subject:David Stew,只针对D列中出现的每个David Stew。这只是我已经拥有的一些数据。

也许您应该替换var dataRange=sheet.getRangestartRow,1,numRows,22,以便只在D列中查看?比如var dataRange=sheet.getRangestartRow,4,numRows,1?并使用它替换您的值,因为您正在替换所有数据表中的ABC。getDataRange.getValues;。目前我无法将var值与特定范围联系起来,我认为它正在搜索整个表。尝试了var values=row[3].getDataRange.getValues;-但它不是这样的,get不能调用方法getDataRange of Undefinedi如果您想从列D中获取值,可以尝试获取行[i][3],以获取所有行,但只有来自DI的值能够指定列,但它在列中添加了两次主题。将调整原始帖子中的代码-有什么问题?可能您的代码在一个元素上传递了两次,并在Subject:ABC中检测到ABC,因此他将其替换为Subject:Subject:ABC