我编写了以下脚本来识别两个文件之间不匹配的名称。这对于第一个循环来说工作得非常好,并且在第二个循环中它完全可以很好地达到 var 提示,但是每当我尝试完成工作表上的提示时,它似乎什么也不做,只是停在那里。这不是因为输入,因为如果我第一次输入该输入,它就会顺利进行。我一定错过了一些关于为什么这不能正确循环的事情。任何帮助将不胜感激
function onEdit(e) {
startPoint();
}
function startPoint(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("README");
var cell = "N5";
var difference = sheet.getRange(cell).getValue().toFixed(2);
if (difference > 0){
yesDifference(difference);
}else noDifference(difference);
}
function yesDifference(num){
const ui = SpreadsheetApp.getUi()
const result = ui.alert(
'There is a difference of: ' +
num
+ '\nWould you like to solve the issue',
ui.ButtonSet.YES_NO)
if (result == ui.Button.YES){
findDifference(num);
}else{
return
}
}
function noDifference(num){
const ui = SpreadsheetApp.getUi()
const result = ui.alert(
'Tips are matching!');
return
}
function findDifference(num){
const ui = SpreadsheetApp.getUi();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("README");
var missingNames = sheet.getRange("Z3:Z20").getValues();
for(var i = 0; i < missingNames.length; i++){
var person = missingNames[i].toString();
if(person.length > 1){
const result = ui.alert(
'I am not able to match:\n' + person + '\nbetween Harri and R365 would you like to try and find them?',
ui.ButtonSet.YES_NO);
if(result == ui.Button.YES){
findNameMatch(person);
}
}
}
return
}
function findNameMatch(name){
const ui = SpreadsheetApp.getUi();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("README");
var allNames = sheet.getRange("A2:A100").getValues();
var filteredNames = [];
for(var i = 0; i < allNames.length; i++){
var person = allNames[i].toString();
if(!(person.length > 1)){
i = allNames.length;
}else{
if(!(filteredNames.includes(person))){
filteredNames.push(person);
}
}
}
var prompt = ui.prompt('Out of the following names:\n\n\n' + filteredNames.join('\r\n') + "\n\n\nPlease enter below which name is supposed to be " + name);
var fullName = prompt.getResponseText().toString();
var resp = ui.alert(fullName);
var firstName = fullName.substring(0, fullName.indexOf(' '));
var lastName = fullName.substring(fullName.indexOf(' ') + 1);
var originalFirst = name.substring(0, name.indexOf(' '));
var originalLast = name.substring(fullName.indexOf(' ') + 1);
var names = ui.alert(
'First Name: ' + firstName + "\nLast Name: " + lastName
)
changeName(originalFirst, firstName, originalLast, lastName);
startPoint();
}
function changeName(oldF, correctF, oldL, correctL){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("365");
var allFNames = sheet.getRange("A2:A100").getValues();
var allLNames = sheet.getRange("B2:B100").getValues();
for(var i = 0; i < allFNames.length; i++){
var name = allFNames[i].toString();
var lastName = allLNames[i].toString();
if(!(name.length > 1)){
i = allFNames.length;
}else{
if((name === oldF) &&(lastName === oldL)){
var newFirst = "A" + (i + 2);
var newLast = "B" + (i + 2);
var newFNames = sheet.getRange(newFirst).setValue(correctF);
var newLNames = sheet.getRange(newLast).setValue(correctL);
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
'The names have been changed at ' + newFirst + ", and " + newLast + " to " + correctF + ", and " + correctL);
i = allFNames.length;
}
}
}
return
}
我创建了一个电子表格,其中包含最少的数据来重现问题。在 R365 表上,如果您编辑其中一个名称,它应该以相同的方式触发该功能
您想要更新与现有数据不匹配的客户姓名,但您的循环不起作用。
考虑这个答案:
差异
'onEdit(e)` 是一个简单触发器,正在超时最大执行时间。
onEdit(e)
更改为其他名称,例如 updateNames(e)
onEdit
触发器。利用事件对象。
Logger.log(JSON.stringify(e))
之后立即插入 updateNames(e)
。
startPoint(e)
和 function startPoint(e){
在
startpoint(e)
中包括对工作表、列和行的测试,例如:if (e.range.getSheet().getName() == "README" && e.range.rowStart == 5 && e.range.columnStart == 14 ){
错误
var originalLast = name.substring(fullName.indexOf(' ') + 1);
var originalLast = name.substring(name.indexOf(' ') + 1);
从
startPoint();
最后一行删除
function findNameMatch(name){
for(var i = 0; i < missingNames.length; i++){
中的
function findDifference
startPoint()
会重新启动整个函数,而不是让“for”循环发挥作用。包含许多
Logger
语句来跟踪变量的值和例程的进度。这些缓慢的处理和 OP 可能希望显示那些在任何时间点看起来有帮助的内容。
尚不清楚“365”表中 M 列的值是否应更新为“差异”。
function updateNames(e) {
// Logger.log(JSON.stringify(e)) // DEBUG
startPoint(e);
}
function startPoint(e){
// test for edited sheet and column and row
if (e.range.getSheet().getName() == "README" && e.range.rowStart == 5 && e.range.columnStart == 14 ){ // cell 'N5"
// edit is in correct place, continue processing
// Logger.log("DEBUG: edit was in N5 on Readme - continue processing")
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("README");
var cell = "N5";
var difference = sheet.getRange(cell).getValue().toFixed(2);
if (difference > 0){
yesDifference(difference);
}else{
noDifference(difference);
}
}
else{
// edit is NOT in correct place, stop processing
// Logger.log("DEBUG: edit was NOT N5 on Readme - stop processing")
return
}
}
function yesDifference(num){
const ui = SpreadsheetApp.getUi()
const result = ui.alert(
'There is a difference of: ' +
num
+ '\nWould you like to solve the issue',
ui.ButtonSet.YES_NO)
if (result == ui.Button.YES){
// solve the difference
// Logger.log("DEBUG: solve the difference")
findDifference(num);
}else{
// do nothing
// Logger.log("DEBUG: Do nothing")
return
}
}
function noDifference(num){
const ui = SpreadsheetApp.getUi()
const result = ui.alert(
'Tips are matching!');
return
}
function findDifference(num){
const ui = SpreadsheetApp.getUi();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("README");
var missingNames = sheet.getRange("Z3:Z20").getValues();
for(var i = 0; i < missingNames.length; i++){
var person = missingNames[i].toString();
if(person.length > 1){
const result = ui.alert(
'I am not able to match:\n' + person + '\nbetween Harri and R365 would you like to try and find them?',
ui.ButtonSet.YES_NO);
if(result == ui.Button.YES){
// Logger.log("DEBUG: the name of the person is "+person)
findNameMatch(person);
}
}
}
return
}
function findNameMatch(name){
const ui = SpreadsheetApp.getUi();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("README");
var allNames = sheet.getRange("A2:A100").getValues();
var filteredNames = [];
for(var i = 0; i < allNames.length; i++){
var person = allNames[i].toString();
if(!(person.length > 1)){
i = allNames.length;
}else{
if(!(filteredNames.includes(person))){
// Logger.log("DEBUG: filtered names doesn't include "+person+", so person added to filtered names")
filteredNames.push(person);
}
}
}
var prompt = ui.prompt('Out of the following names:\n\n\n' + filteredNames.join('\r\n') + "\n\n\nPlease enter below which name is supposed to be " + name);
var fullName = prompt.getResponseText().toString();
var resp = ui.alert(fullName);
// Logger.log("DEBUG: name chosen for findNameMatch = "+fullName)
var firstName = fullName.substring(0, fullName.indexOf(' '));
var lastName = fullName.substring(fullName.indexOf(' ') + 1);
var originalFirst = name.substring(0, name.indexOf(' '));
var originalLast = name.substring(name.indexOf(' ') + 1);
// Logger.log("DEBUG: findNameMatch: first name = "+firstName+", last name = "+lastName+", Original first = "+originalFirst+", Original last = "+originalLast)
var names = ui.alert(
'First Name: ' + firstName + "\nLast Name: " + lastName
)
changeName(originalFirst, firstName, originalLast, lastName);
}
function changeName(oldF, correctF, oldL, correctL){
// Logger.log("DEBUG: changename: oldF = "+oldF+", correct F = "+correctF+", oldL = "+oldL+", correct L = "+correctL)
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("365");
var allFNames = sheet.getRange("A2:A100").getValues();
var allLNames = sheet.getRange("B2:B100").getValues();
for(var i = 0; i < allFNames.length; i++){
var name = allFNames[i].toString();
var lastName = allLNames[i].toString();
// Logger.log("DEBUG: changeName: i:"+i+", name:"+name+", last name:"+lastName+", name length:"+name.length)
if(!(name.length > 1)){
i = allFNames.length;
}else{
if((name === oldF) &&(lastName === oldL)){
// Logger.log("DEBUG: name <> old F and last name <> oldL")
var newFirst = "A" + (i + 2);
var newLast = "B" + (i + 2);
// Logger.log("DEBUG: cells: newFirst:"+newFirst+", newlast:"+newLast)
var newFNames = sheet.getRange(newFirst).setValue(correctF);
var newLNames = sheet.getRange(newLast).setValue(correctL);
// Logger.log("DEBUG: Updated newfirst and newlast with correctF and correctL")
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
'The names have been changed at ' + newFirst + ", and " + newLast + " to " + correctF + ", and " + correctL);
i = allFNames.length;
// Logger.log("DEBUG: i is set to allFnames length: "+i)
}else{
// Logger.log("DEBUG: if unsuccessful")
}
}
}
return
}
样本数据 - 之前的“Harri”表
样本数据 - 表“365:之前
示例数据 - 第 2 人
示例数据 - 第 4 人