拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如果字符串匹配,则将值从一行复制到另一行

如果字符串匹配,则将值从一行复制到另一行

白鹭 - 2022-01-23 2121 0 0

如果表单中设定的邮件与存盘在资料行中的邮件匹配,我需要复制整行 Google 表格。这是我的代码示例,但问题是当它为“真”并且邮件与表格上的邮件匹配时,它会复制最后一行而不是邮件匹配的那一行。

例子:

|Nombre y apellido completo:|   DNI:  | Correo electrónico |
| ------------------------- |-------- |--------------------|
|sdasdas                    | 3213213 | test1@gmail.com    |
|sdasdas                    | 3213213 | test2@gmail.com    |

test1@gmail.com 是有效的,所以它应该复制 test1 但在我的情况下是复制 test2 资料行。

|Nombre y apellido completo:|   DNI:  | Correo electrónico |
| ------------------------- |-------- |--------------------|
|sdasdas                    | 3213213 | test1@gmail.com    |
|sdasdas                    | 3213213 | test2@gmail.com    |
|sdasdas                    | 3213213 | test2@gmail.com    | 

它应该说 test1@gmail.com

deleteTriggers()

var form = FormApp.getActiveForm();
ScriptApp.newTrigger('selectedItems')
    .forForm(form)
    .onFormSubmit()
    .create();

function selectedItems() {

    var form = FormApp.getActiveForm();
    var formResponses = form.getResponses();
    var formResponse = formResponses[formResponses.length - 1];
    var itemResponses = formResponse.getItemResponses();

    var name = "";
    var email = "";
    var gender = "";
    var choice = "";
    var usedEmail = "";

    for (var i = 0; i < itemResponses.length; i  ) {
        switch (itemResponses[i].getItem().getTitle()) {
            case "Correo electrónico":
                email = itemResponses[i].getResponse();
                break;
            case "Sexo:":
                gender = itemResponses[i].getResponse();
                break;
            case "Nombre completo:":
                name = itemResponses[i].getResponse();
                break;
            case "?Estuviste antes en una reunión?":
                choice = itemResponses[i].getResponse();
            default:
            case "Correo electrónico usado:":
                usedEmail = itemResponses[i].getResponse();
                break;
        }
    }


    getItems(email, usedEmail, choice, name, gender)

}


function getItems(email, usedEmail, choice, name, gender) {
    var spreadSheet = SpreadsheetApp.openById(form.getDestinationId());
    var spreadSheetData = spreadSheet.getDataRange()
    var rowValue = spreadSheetData.getValues()
    Logger.log(rowValue)
    var bool

    if (choice == "Sí") {
        for (var i = 0; i < rowValue.length; i  ) {
            for (var j = 0; j < rowValue[i].length; j  ) {
                if (usedEmail.indexOf(rowValue[i][j]) != -1) {
                    rowValue[i][0] = new Date()
                    var rowValues = rowValue[i]

                    Logger.log(rowValue[i])
                    bool = true
                } else {
                    bool = false
                }
            }
        }
    }

    Logger.log(usedEmail)
    Logger.log(bool)
    if (bool == true) {
        setItems(spreadSheet, rowValues)
        //RespuestaAutomatica(name, email, gender)
        Logger.log("Inscripción válida")
    } else {
        Logger.log("Inscripción fallida, email no registrado")
    }

}

function setItems(spreadSheet, rowValues) {
    spreadSheet.appendRow(rowValues)
}

function deleteTriggers() {
    var allTriggers = ScriptApp.getProjectTriggers();
    for (var i = 0; i < allTriggers.length; i  ) {
        ScriptApp.deleteTrigger(allTriggers[i]);
    }
}

uj5u.com热心网友回复:

尝试对您的脚本进行此修改。详情见评论。

修改:

function getItems(email, usedEmail, choice, name, gender) {
  var spreadSheet = SpreadsheetApp.openById(form.getDestinationId());
  var spreadSheetData = spreadSheet.getDataRange()
  var rowValue = spreadSheetData.getValues()

  // CHANGES START HERE:
  // find what column is your emails located, I assume the header is 'Correo electrónico'
  var emailHeaderCol = rowValue[0].indexOf('Correo electrónico')
  // create array for email column
  var existingEmails = rowValue.map(x => x[emailHeaderCol])
  
  if (choice == "Sí") {
    var index = existingEmails.indexOf(usedEmail)
    if (index > -1) {
      rowValue[index][0] = new Date()
      var rowValues = rowValue[index]
      bool = true
    }
    else {
      bool = false
    }
  }
  // CHANGES END HERE:

  if (bool == true) {
      setItems(spreadSheet, rowValues)
      //RespuestaAutomatica(name, email, gender)
      Logger.log("Inscripción válida")
  } else {
      Logger.log("Inscripción fallida, email no registrado")
  }
}

输出:

如果字符串匹配,则将值从一行复制到另一行

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *