【问题标题】:Overwriting Google sheets (for form response) rows if duplicate entered如果输入重复,则覆盖 Google 表格(用于表单响应)行
【发布时间】:2018-06-25 12:31:38
【问题描述】:

所以,我一直在试图弄清楚如何阻止重复行出现在我的 google 表单响应输出中的 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;}
}

【问题讨论】:

  • 是什么触发了这段代码运行?函数名称是否与“提交表单”触发器相关联?你知道代码是否正在运行吗?
  • 到目前为止,我一直在尝试使用 Run/imported as macro 让代码成功运行。它将设置为在您提到的每个提交触发器上运行-我现在将尝试使用您的代码,感谢您抽出时间来帮助我:)

标签: google-apps-script google-sheets google-forms google-sheets-macros


【解决方案1】:

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

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

【讨论】:

  • 所以,我将您编写的代码('updateExisting')作为宏导入到该 3 行测试表中。每当它运行时,都会删除顶部(唯一)行并保留 2 个相同的行 - 我想我一定是做错了什么。我已经硬编码了正确的工作表名称,并将唯一列设置为 2(电子邮件)。
  • 代码中有错误。您使用的代码可能覆盖了错误的行。在倒数第二行中,将“rowWithExistingUniqueValue”变量加 1:sh.getRange(rowWithExistingUniqueValue + 1, 另外,取消注释 Logger.log() 语句,运行代码,然后在 View 菜单中选择 Logs。
  • 太棒了,您的编辑效果很好,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
相关资源
最近更新 更多