英文:
How can I check for duplicates in multiple Google Sheets workbooks in the same directory?
问题
我有一个用于输入数据的Google表格,我想要检查输入的数据是否在同一目录中的其他Google表格工作簿中重复。如果数据在任何其他工作簿中找到,我想要突出显示重复的单元格以显示它是重复的。
我找到了一个在线脚本,可以用于检查同一工作表中的重复项,但我不确定如何修改它以检查同一目录中其他工作簿中的重复项。
以下是我找到的脚本:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
var value = range.getValue();
var data = sheet.getDataRange().getValues();
var row = range.getRow() - 1;
var col = range.getColumn() - 1;
// 遍历工作表中的所有单元格
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
if (i == row && j == col) {
continue; // 跳过编辑的单元格
}
if (data[i][j] == value) {
// 如果在同一工作表中找到重复项,突出显示单元格
sheet.getRange(i + 1, j + 1).setBackground("#ff0000"); // 更改颜色代码为您喜欢的颜色
}
}
}
}
请问如何修改此脚本以检查同一目录中其他工作簿中的重复项?谢谢!
英文:
I have a Google Sheet that I use to enter data, and I want to check if the entered data is a duplicate in any of the other Google Sheets workbooks in the same directory. If the data is found in any other workbook, I want to highlight the duplicate cell to show that it's a duplicate.
I found a script online that works for checking duplicates in the same sheet, but I'm not sure how to modify it to check for duplicates in other workbooks in the same directory.
Here's the script I found:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
var value = range.getValue();
var data = sheet.getDataRange().getValues();
var row = range.getRow() - 1;
var col = range.getColumn() - 1;
// loop through all cells in the sheet
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
if (i == row && j == col) {
continue; // skip the edited cell
}
if (data[i][j] == value) {
// highlight the cell if a duplicate is found in the same sheet
sheet.getRange(i + 1, j + 1).setBackground("#ff0000"); //change the color code to whatever color you prefer
}
}
}
}
Any help on how to modify this script to check for duplicates in other workbooks in the same directory would be greatly appreciated! Thanks in advance.
答案1
得分: 2
以下是您要求的翻译部分:
I believe your goal is as follows.
我相信您的目标如下。
- When a value is put into a cell on the active Spreadsheet, you want to set the background color of the cell of the duplicated value to
#ff0000
. - As an additional function, you want to reflect this in all Spreadsheets in the same folder of the active Spreadsheet.
- 当在活动电子表格中的单元格中输入一个值时,您希望将重复值的单元格背景颜色设置为
#ff0000
。 - 作为附加功能,您希望在活动电子表格所在文件夹中的所有电子表格中反映这一操作。
In this case, how about the following sample script?
在这种情况下,以下示例脚本如何?
Sample script:
示例脚本:
In this script, when you put a value to a cell, the inputted cell value is searched from all Spreadsheets in the same folder of the active Spreadsheet. And, when the duplicated values are found, the background of the cells is changed to #ff0000
. In order to search the values, in this sample, I used TextFinder.
在这个脚本中,当您向单元格中输入一个值时,该值将从活动电子表格所在文件夹中的所有电子表格中搜索。当找到重复的值时,单元格的背景颜色会更改为 #ff0000
。为了搜索这些值,在这个示例中,我使用了TextFinder。
Note:
注意:
- In this sample script, the inputted value is searched from all sheets in each Spreadsheet. If you want to search it from the specific sheet, please modify
sheckDuplicate(source);
tosheckDuplicate(source.getActiveSheet());
andsheckDuplicate(SpreadsheetApp.open(file));
tosheckDuplicate(SpreadsheetApp.open(file).getSheetByName("###sheetName###"));
. - 在这个示例脚本中,输入的值将从每个电子表格的所有工作表中搜索。如果您想要从特定工作表中搜索,请修改
sheckDuplicate(source);
为sheckDuplicate(source.getActiveSheet());
和sheckDuplicate(SpreadsheetApp.open(file));
为sheckDuplicate(SpreadsheetApp.open(file).getSheetByName("###sheetName###"));
。
References:
参考链接:
- Installable Triggers
- createTextFinder(findText) of Class Spreadsheet
- createTextFinder(findText) of Class Sheet
- Installable Triggers
- 电子表格类的 createTextFinder(findText) 方法
- 工作表类的 createTextFinder(findText) 方法
Added:
附加:
From your reply, I understood your expected result is as follows.
根据您的回复,我了解到您期望的结果如下。
- From your reply, when you put a value to the active sheet, you want to search the inputted value from all sheets in the active Spreadsheet, and also, you want to search all sheets in all Spreadsheets under the same parent folder of the active Spreadsheet. When the inputted value is found from other sheets, you want to set the background color of the inputted cell and you don't want to change the background color of all other sheets.
- 根据您的回复,当您将一个值放入活动工作表时,您希望从活动电子表格中的所有工作表中搜索输入的值,还希望从活动电子表格所在文件夹中的所有电子表格中搜索所有工作表。当从其他工作表中找到输入的值时,您希望设置输入单元格的背景颜色,并且您不希望更改所有其他工作表的背景颜色。
In this case, the sample script is as follows.
在这种情况下,示例脚本如下。
Sample script:
示例脚本:
In this case, also, the installable OnEdit trigger is used. So, please install it to installedOnEdit
, and put a value in the cell.
在这种情况下,同样使用了可安装的OnEdit触发器。因此,请将其安装到 installedOnEdit
,然后在单元格中输入一个值。
function installedOnEdit(e) {
const { range, source } = e;
const ssId = source.getId();
const value = range.getDisplayValue();
if (!value) return;
// For active Spreadsheet.
let check = source.createTextFinder(value).matchEntireCell(true).findAll().length > 1 ? true : false;
// For Spreadsheets except for active Spreadsheet in the same folder.
if (!check) {
const parentFolder = DriveApp.getFileById(ssId).getParents().next();
const files = parentFolder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
const file = files.next();
if (file.getId() != ssId) {
check = SpreadsheetApp.open(file).createTextFinder(value).matchEntireCell(true).findNext();
if (check) break;
}
}
}
if (!check) return;
range.setBackground("#ff0000");
}
英文:
I believe your goal is as follows.
- When a value is put into a cell on the active Spreadsheet, you want to set the background color of the cell of the duplicated value to
#ff0000
. - As an additional function, you want to reflect this in all Spreadsheets in the same folder of the active Spreadsheet.
In this case, how about the following sample script?
Sample script:
In this sample, in order to use the Drive service (DriveApp) and SpreadsheetApp.open
, please install the OnEdit trigger to function installedOnEdit
. Ref
When this script is run, please edit a cell. By this, the script is run by the installable OnEdit trigger.
function installedOnEdit(e) {
const { range, source } = e;
const a1Notation = range.getA1Notation();
const sheetName = source.getActiveSheet().getSheetName();
const ssId = source.getId();
const value = range.getValue();
// For active Spreadsheet.
const sheckDuplicate = spreadsheet => {
const ranges = spreadsheet.createTextFinder(value).matchEntireCell(true).findAll();
ranges.forEach(r => {
if (r.getSheet().getSheetName() != sheetName || r.getA1Notation() != a1Notation) {
r.setBackground("#ff0000");
}
});
};
sheckDuplicate(source);
// For Spreadsheets except for active Spreadsheet in the same folder.
const parentFolder = DriveApp.getFileById(ssId).getParents().next();
const files = parentFolder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
const file = files.next();
if (file.getId() != ssId) {
sheckDuplicate(SpreadsheetApp.open(file));
}
}
}
- In this script, when you put a value to a cell, the inputted cell value is searched from all Spreadsheets in the same folder of the active Spreadsheet. And, when the duplicated values are found, the background of the cells is changed to
#ff0000
. In order to search the values, in this sample, I used TextFinder.
Note:
- In this sample script, the inputted value is searched from all sheets in each Spreadsheet. If you want to search it from the specific sheet, please modify
sheckDuplicate(source);
tosheckDuplicate(source.getActiveSheet());
andsheckDuplicate(SpreadsheetApp.open(file));
tosheckDuplicate(SpreadsheetApp.open(file).getSheetByName("###sheetName###"));
.
References:
- Installable Triggers
- createTextFinder(findText) of Class Spreadsheet
- createTextFinder(findText) of Class Sheet
Added:
From your reply, I understood your expected result is as follows.
- From your reply, when you put a value to the active sheet, you want to search the inputted value from all sheets in the active Spreadsheet, and also, you want to search all sheets in all Spreadsheets under the same parent folder of the active Spreadsheet. When the inputted value is found from other sheets, you want to set the background color of the inputted cell and you don't want to change the background color of all other sheets.
In this case, the sample script is as follows.
Sample script:
In this case, also, the installable OnEdit trigger is used. So, please install it to installedOnEdit
, and put a value in the cell.
function installedOnEdit(e) {
const { range, source } = e;
const ssId = source.getId();
const value = range.getDisplayValue();
if (!value) return;
// For active Spreadsheet.
let check = source.createTextFinder(value).matchEntireCell(true).findAll().length > 1 ? true : false;
// For Spreadsheets except for active Spreadsheet in the same folder.
if (!check) {
const parentFolder = DriveApp.getFileById(ssId).getParents().next();
const files = parentFolder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
const file = files.next();
if (file.getId() != ssId) {
check = SpreadsheetApp.open(file).createTextFinder(value).matchEntireCell(true).findNext();
if (check) break;
}
}
}
if (!check) return;
range.setBackground("#ff0000");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论