Googleのスプレッドシートで検索と置換を実行するには、以下のコードを使用しています。それはstackoverflowで見つけた答えに基づいています。Google Apps Script:特定の列のみを検索して置き換えます
私の問題は、スプレッドシートの特定の1つの列に対してのみ実行させることができないことです。
D列のABCを検索し、ABCをD列のSubject:ABCに置き換えたいとします。
私はvar values = row [3]を定義しようとしました。しかし、それはエラーをスローします(メソッドsetValues(文字列)を見つけることができません)(行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;
}
}
EDIT
これは動作しますが、それは、二度、すなわち件名を件名を追加します:件名: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);
}
}
おそらく、あなたのvar dataRange = sheet.getRange(startRow、1、numRows、22)をD列だけに置き換えるべきでしょうか? var dataRange = sheet.getRange(startRow、4、numRows、1)のように?それを使用して値を置き換えて、すべてのデータ(sheet.getDataRange()。getValues();)でABCを置き換えます。 –
現時点では、var値を特定の範囲に結びつけることはできませんが、シート全体を検索しています。 var values = row [3] .getDataRange()。getValues();のようなものを試してみました。 - それは好きではない、 "getDataRange"は未定義のgetDataRange "を呼び出すことはできません。 – touto
列Dから値を取得したい場合は、行[i] [3]を取得してすべての行を取得し、 D –