Google Apps脚本:仅在特定列中查找和替换

时间:2017-05-10 11:51:14

标签: google-apps-script google-sheets

我使用下面的代码在Google电子表格中运行搜索和替换。它基于我在stackoverflow找到的答案。

我的问题是我无法让它仅针对电子表格中的一个特定列运行。

我想在D栏中搜索ABC,然后仅在D栏中用主题:ABC替换ABC。

我尝试过定义var values = row [3];但是会抛出一个错误(找不到方法setValues(string)。(第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);        

}
}

1 个答案:

答案 0 :(得分:0)

这里有一些适合我的代码。

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);
}

这是我的数据所在。

enter image description here

我在D栏中搜索了David Stew,并且在D列中每次出现David Stew时都用Subject:David Stew替换了该字符串。这只是我已经拥有的一些数据。