首页 > 解决方案 > 如果输入重复,则覆盖 Google 表格(用于表单响应)行

问题描述

所以,我一直在试图弄清楚如何停止出现在我的谷歌表单响应输出中的重复行。如果发现这个链接听起来完全符合我的要求(Form Google Script Prevent Duplicates),但我一生都无法弄清楚如何编辑给定的答案以在我的工作表上工作。

我已经包含了我的工作簿的屏幕截图,以举例说明我希望在其上运行编辑过的代码的数据结构,下面是我尝试使代码在我的数据结构上正确运行的尝试。

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

我尝试调整代码以处理上述数据结构(我完全没有这种脚本语言的背景,所以如果我犯了一个明显的错误,请放轻松):

function updateExisting() {
  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(), 
      row, len;

  // TRY AND FIND EXISTING NAME
  for (row = 0, len = allNames.length; row < len - 1; row++)
    if (allNames[row][0] == name) {
      // OVERWRITE OLD DATA
      s.getRange('A2').offset(0, 0, row, 
lastValues.length).setValues([lastValues]);
      // DELETE THE LAST ROW
      s.deleteRow(lastRow);
      break;}
}

标签: google-apps-scriptgoogle-sheetsgoogle-formsgoogle-sheets-macros

解决方案


关键词:重复、谷歌、电子表格、表格、表单、提交、编辑、行、唯一。

此代码通过使用现有唯一值(如果存在)覆盖现有行来防止在提交 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
}

推荐阅读