Google脚本防止重复 [英] Form Google Script Prevent Duplicates

查看:94
本文介绍了Google脚本防止重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作Google表单,并且有一个名称与其他字段(如标题,公司和电子邮件地址)相关的字段。如果数据库中已有特定人员,我希望其他信息用新信息(即更新功能)替换旧信息,但由于我找到文档,因此在使用Google Apps脚本时遇到问题相当可悲。有人会介意给我一只手吗?

解决方案

这不会阻止Google Form在第一次提交重复值我认为你想要的东西会看起来像......

  function updateExisting(){
var ss = SpreadsheetApp.getActiveSpreadsheet(),
s = ss.getSheetByName('Sheet1'),
lastRow = s.getLastRow(),
lastValues = s.getRange('A'+ lastRow +' :E'+ lastRow).getValues(),
name = lastValues [0] [0],
allNames = s.getRange('A2:A')。getValues(),
行,len;

//尝试并找到现有名称
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]);
//删除最后一行
s.deleteRow(lastRow);
break;}
}

这必须由表单在您的工作表内提交触发器。



文档可能会令人难以置信。他们通常只做1或2行示例,但如果您运行所有教程还有很多完成的例子。更多的是开发人员制作这些类型的脚本。

I'm making a google form and I have a field called name with other fields like title, company, and email address. If there is already a specific person in the database, I want the other information to replace the old information with the new info (i.e. an update function), but I'm having trouble doing with that with Google Apps Script since I find the documentation rather pathetic. Would anyone mind giving me a hand?

解决方案

This won't prevent a Google Form from getting submitted with duplicate values in the first place, but I think what you want will look some thing like...

function updateExisting() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      s = ss.getSheetByName('Sheet1'),
      lastRow = s.getLastRow(),
      lastValues = s.getRange('A'+lastRow+':E'+lastRow).getValues(),
      name = lastValues[0][0],
      allNames = s.getRange('A2:A').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;}
}

This has to be triggered by the on Form Submit trigger inside your Sheet.

Docs can be overwhelming. They typically just do a 1 or 2 line examples, although if you run through all the tutorials there's a lot more finished examples. It's more on the developers to make these types of scripts.

这篇关于Google脚本防止重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆