Google 用于 Google 工作表中多个查找和替换的 Apps 脚本
Google Apps Script for Multiple Find and Replace in Google Sheets
关于 Stack Exchange 的第一个问题,希望它有意义。
一些背景:我在学校环境中工作,正在协助学习支持人员为某些学生创建更易读的时间表。
他们正在从我们的网站上复制时间表数据,其中包含科目代码、教师姓名和房间号。它的格式与您在下图中看到的格式完全相同 - 我只是将其复制到 Google 表格中。
我基本上需要对所有这些代码执行批量查找和替换,并完全扩展它们,以便主题代码,例如01ENG02 变为 'English' 和教师代码,例如JBO 变成 "Joe Bloggs"
我有一份完整的列表,列出了我需要将代码扩展到的目标 - 这就是实现它的最佳方式。
这是我在 Stack Exchange 和我正在使用的其他网站上找到的一些 Google 脚本代码:
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("StudentTimetableEntry");
// Replace Subject Names
replaceInSheet(sheet, /\d\dART\d\d/g, "Art");
replaceInSheet(sheet, /\d\dCCL\d\d/g, "Communication & Culture");
replaceInSheet(sheet, /\d\dDLT\d\d/g, "Digital Technology");
replaceInSheet(sheet, /\d\dDRA\d\d/g, "Drama");
// Replace Staff Names
replaceInSheet(sheet, 'TED', 'Tahlee Edward');
replaceInSheet(sheet, 'TLL', 'Tyrone LLoyd');
replaceInSheet(sheet, 'TMA', 'Timothy Mahone');
replaceInSheet(sheet, 'TQU', 'Tom Quebec');
}
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);
}
这非常有效。但是,我有 150 多个员工姓名,以及大致相同数量的主题代码。该过程达到了最长时间,我相信一定有更好的编码方式。
我会考虑其他方法,请记住,对于将要使用它的员工来说,它需要尽可能简单明了。
每次您在脚本中调用 getValues
和 setValues
时,都会涉及相当大的开销成本并减慢脚本速度。 (Google app script timeout ~ 5 minutes?) 我修改了您上面的脚本,使 1 次调用 getValues
和 1 次调用 setValues
。在将修改后的时间表粘贴回 sheet.
之前,代码将所有替换应用于内存中的数组
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("StudentTimetableEntry");
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getDataRange().getValues();
// Replace Subject Names
replaceInSheet(values, /\d\dART\d\d/g, "Art");
replaceInSheet(values, /\d\dCCL\d\d/g, "Communication & Culture");
replaceInSheet(values, /\d\dDLT\d\d/g, "Digital Technology");
replaceInSheet(values, /\d\dDRA\d\d/g, "Drama");
// Replace Staff Names
replaceInSheet(values, 'TED', 'Tahlee Edward');
replaceInSheet(values, 'TLL', 'Tyrone LLoyd');
replaceInSheet(values, 'TMA', 'Timothy Mahone');
replaceInSheet(values, 'TQU', 'Tom Quebec');
// Write all updated values to the sheet, at once
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;
}
}
试试这段代码,如果您仍然遇到超时问题,我的建议是设置触发器以帮助链式函数。
关于 Stack Exchange 的第一个问题,希望它有意义。
一些背景:我在学校环境中工作,正在协助学习支持人员为某些学生创建更易读的时间表。
他们正在从我们的网站上复制时间表数据,其中包含科目代码、教师姓名和房间号。它的格式与您在下图中看到的格式完全相同 - 我只是将其复制到 Google 表格中。
我基本上需要对所有这些代码执行批量查找和替换,并完全扩展它们,以便主题代码,例如01ENG02 变为 'English' 和教师代码,例如JBO 变成 "Joe Bloggs"
我有一份完整的列表,列出了我需要将代码扩展到的目标 - 这就是实现它的最佳方式。
这是我在 Stack Exchange 和我正在使用的其他网站上找到的一些 Google 脚本代码:
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("StudentTimetableEntry");
// Replace Subject Names
replaceInSheet(sheet, /\d\dART\d\d/g, "Art");
replaceInSheet(sheet, /\d\dCCL\d\d/g, "Communication & Culture");
replaceInSheet(sheet, /\d\dDLT\d\d/g, "Digital Technology");
replaceInSheet(sheet, /\d\dDRA\d\d/g, "Drama");
// Replace Staff Names
replaceInSheet(sheet, 'TED', 'Tahlee Edward');
replaceInSheet(sheet, 'TLL', 'Tyrone LLoyd');
replaceInSheet(sheet, 'TMA', 'Timothy Mahone');
replaceInSheet(sheet, 'TQU', 'Tom Quebec');
}
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);
}
这非常有效。但是,我有 150 多个员工姓名,以及大致相同数量的主题代码。该过程达到了最长时间,我相信一定有更好的编码方式。
我会考虑其他方法,请记住,对于将要使用它的员工来说,它需要尽可能简单明了。
每次您在脚本中调用 getValues
和 setValues
时,都会涉及相当大的开销成本并减慢脚本速度。 (Google app script timeout ~ 5 minutes?) 我修改了您上面的脚本,使 1 次调用 getValues
和 1 次调用 setValues
。在将修改后的时间表粘贴回 sheet.
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("StudentTimetableEntry");
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getDataRange().getValues();
// Replace Subject Names
replaceInSheet(values, /\d\dART\d\d/g, "Art");
replaceInSheet(values, /\d\dCCL\d\d/g, "Communication & Culture");
replaceInSheet(values, /\d\dDLT\d\d/g, "Digital Technology");
replaceInSheet(values, /\d\dDRA\d\d/g, "Drama");
// Replace Staff Names
replaceInSheet(values, 'TED', 'Tahlee Edward');
replaceInSheet(values, 'TLL', 'Tyrone LLoyd');
replaceInSheet(values, 'TMA', 'Timothy Mahone');
replaceInSheet(values, 'TQU', 'Tom Quebec');
// Write all updated values to the sheet, at once
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;
}
}
试试这段代码,如果您仍然遇到超时问题,我的建议是设置触发器以帮助链式函数。