Google apps script 如果输入了重复项,则覆盖Google工作表(用于表单响应)行

Google apps script 如果输入了重复项,则覆盖Google工作表(用于表单响应)行,google-apps-script,google-sheets,google-forms,google-sheets-macros,Google Apps Script,Google Sheets,Google Forms,Google Sheets Macros,因此,我一直在试图找出如何阻止从谷歌表单输出的谷歌表单响应中出现重复行。如果找到这个链接,它听起来像是做了我想要的(),但我的生活无法解决如何编辑给定的答案在我的工作表上工作 我已经包括了我的工作簿的屏幕截图,以给出我希望编辑的代码能够运行的数据结构的示例,下面是我在数据结构上正确运行代码的尝试 我希望在其上运行代码的工作表结构。我想使用电子邮件地址作为“唯一”标识符,因此可以使用该标识符识别任何重复的行 我试图调整代码以处理上述数据结构(我完全没有这种脚本语言的背景,因此如果我犯了明显的错误,

因此,我一直在试图找出如何阻止从谷歌表单输出的谷歌表单响应中出现重复行。如果找到这个链接,它听起来像是做了我想要的(),但我的生活无法解决如何编辑给定的答案在我的工作表上工作

我已经包括了我的工作簿的屏幕截图,以给出我希望编辑的代码能够运行的数据结构的示例,下面是我在数据结构上正确运行代码的尝试

我希望在其上运行代码的工作表结构。我想使用电子邮件地址作为“唯一”标识符,因此可以使用该标识符识别任何重复的行

我试图调整代码以处理上述数据结构(我完全没有这种脚本语言的背景,因此如果我犯了明显的错误,请对我宽容一些):

函数更新存在(){
var s=SpreadsheetApp.getActiveSheet(),
//s=ss.getSheetByName(“”),
lastRow=s.getLastRow(),
lastValues=s.getRange('A'+lastRow+':C'+lastRow)。getValues(),
name=lastValues[0][0],
allNames=s.getRange('B2:B').getValues(),
罗,兰;
//尝试查找现有名称
for(row=0,len=allNames.length;row
关键词:重复、谷歌、电子表格、工作表、表单、提交、编辑、行、唯一

此代码通过使用现有唯一值(如果存在)覆盖现有行,防止提交Google表单时Google工作表中出现重复。 代码在电子表格中搜索一列并查找匹配项。我试图使其通用化,这样代码就不需要根据唯一标识符所在的列进行更改。您需要在“用户设置”部分进行一些设置,以使其正常工作。但这比需要重写代码要好

function updateExisting(columnWithUniqueIdentifier,sheetTabName) {
  var dataFromColumnToMatch,lastColumn,lastRow,rowWithExistingUniqueValue,rowOfDataJustSaved,
      sh,ss,valueToSearchFor;

  // USER SETTINGS - if the values where not passed in to the function
  if (!columnWithUniqueIdentifier) {//If you are not passing in the column number
    columnWithUniqueIdentifier = 2;//Hard code column number if you want
  }

  if (!sheetTabName) {//The sheet tab name was not passed in to the function
    sheetTabName = "Put your Sheet tab name here";//Hard code if needed
  }
  //end of user settings

  ss = SpreadsheetApp.getActiveSpreadsheet();//Get the active spreadsheet - this code must be in a project bound to spreadsheet
  sh = ss.getSheetByName(sheetTabName);

  lastRow = sh.getLastRow();
  lastColumn = sh.getLastColumn();

  //Logger.log('lastRow: ' + lastRow)

  rowOfDataJustSaved = sh.getRange(lastRow, 1, 1, lastColumn).getValues();//Get the values that were just saved

  valueToSearchFor = rowOfDataJustSaved[0][columnWithUniqueIdentifier-1];
  //Logger.log('valueToSearchFor: ' + valueToSearchFor)

  dataFromColumnToMatch = sh.getRange(1, columnWithUniqueIdentifier, lastRow-1, 1).getValues();
  dataFromColumnToMatch = dataFromColumnToMatch.toString().split(",");
  //Logger.log('dataFromColumnToMatch: ' + dataFromColumnToMatch)

  rowWithExistingUniqueValue = dataFromColumnToMatch.indexOf(valueToSearchFor);
  //Logger.log('rowWithExistingUniqueValue: ' + rowWithExistingUniqueValue)

  if (rowWithExistingUniqueValue === -1) {//There is no existing data with the unique identifier
    return;
  }

  sh.getRange(rowWithExistingUniqueValue + 1, 1, 1, rowOfDataJustSaved[0].length).setValues(rowOfDataJustSaved);
  sh.deleteRow(lastRow);//delete the row that was at then end
}

是什么触发此代码运行?函数名是否与“表单提交”触发器关联?您知道代码是否正在运行吗?到目前为止,我一直在尝试使用run/imported as宏使代码成功运行。它将被设置为在您提到的每个提交触发器上运行-我现在将尝试使用您的代码,感谢您花时间帮助我:)因此,我将您编写的代码('updateExisting')作为宏导入到3行测试表中。每当它运行时,最上面的(唯一的)行被删除,两个相同的行被保留——我想我一定是做错了什么。我已硬编码正确的工作表名称,并将唯一列设置为2(电子邮件)。代码中有错误。您正在使用的代码可能覆盖了错误的行。在最后一行的下一行中,将1添加到“rowWithExistingUniqueValue”变量中:
sh.getRange(rowWithExistingUniqueValue+1,
另外,取消对
Logger.log()
语句的注释,运行代码,然后在“视图”菜单中选择“日志”。令人惊讶的是,您的编辑工作非常出色,非常感谢!
function updateExisting(columnWithUniqueIdentifier,sheetTabName) {
  var dataFromColumnToMatch,lastColumn,lastRow,rowWithExistingUniqueValue,rowOfDataJustSaved,
      sh,ss,valueToSearchFor;

  // USER SETTINGS - if the values where not passed in to the function
  if (!columnWithUniqueIdentifier) {//If you are not passing in the column number
    columnWithUniqueIdentifier = 2;//Hard code column number if you want
  }

  if (!sheetTabName) {//The sheet tab name was not passed in to the function
    sheetTabName = "Put your Sheet tab name here";//Hard code if needed
  }
  //end of user settings

  ss = SpreadsheetApp.getActiveSpreadsheet();//Get the active spreadsheet - this code must be in a project bound to spreadsheet
  sh = ss.getSheetByName(sheetTabName);

  lastRow = sh.getLastRow();
  lastColumn = sh.getLastColumn();

  //Logger.log('lastRow: ' + lastRow)

  rowOfDataJustSaved = sh.getRange(lastRow, 1, 1, lastColumn).getValues();//Get the values that were just saved

  valueToSearchFor = rowOfDataJustSaved[0][columnWithUniqueIdentifier-1];
  //Logger.log('valueToSearchFor: ' + valueToSearchFor)

  dataFromColumnToMatch = sh.getRange(1, columnWithUniqueIdentifier, lastRow-1, 1).getValues();
  dataFromColumnToMatch = dataFromColumnToMatch.toString().split(",");
  //Logger.log('dataFromColumnToMatch: ' + dataFromColumnToMatch)

  rowWithExistingUniqueValue = dataFromColumnToMatch.indexOf(valueToSearchFor);
  //Logger.log('rowWithExistingUniqueValue: ' + rowWithExistingUniqueValue)

  if (rowWithExistingUniqueValue === -1) {//There is no existing data with the unique identifier
    return;
  }

  sh.getRange(rowWithExistingUniqueValue + 1, 1, 1, rowOfDataJustSaved[0].length).setValues(rowOfDataJustSaved);
  sh.deleteRow(lastRow);//delete the row that was at then end
}