You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过多URL参数匹配删除Google Sheet对应行?

双条件匹配删除Google Sheet行的解决方案

修改Google Apps Script代码

原代码仅通过邮箱匹配删除行,现在调整为同时校验姓名和邮箱两个条件,修改后的代码如下:

const sheetName = "Sheet1"; // 替换为你的工作表名称

function doPost(e) {
  // 提取URL参数中的姓名、邮箱和操作标识
  const { Name: name, Email: email, sample } = e.parameter;
  
  if (sample === "delete") {
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
    const dataRange = sheet.getDataRange();
    const values = dataRange.getValues();
    const targetRows = [];

    // 遍历所有数据行(跳过表头,假设第1行是表头)
    for (let i = 1; i < values.length; i++) {
      const currentRow = values[i];
      // 请根据你的表格结构修改列索引:这里第1列(索引0)是姓名,第2列(索引1)是邮箱
      const rowName = currentRow[0];
      const rowEmail = currentRow[1];
      
      // 匹配姓名和邮箱完全一致的行
      if (rowName === name && rowEmail === email) {
        // 记录实际行号(数组索引i对应表格第i+1行)
        targetRows.push(i + 1);
      }
    }

    if (targetRows.length === 0) {
      return ContentService.createTextOutput(`未找到姓名为"${name}"且邮箱为"${email}"的记录。`);
    }

    // 倒序删除行,避免删除前面的行导致后续行号偏移
    targetRows.reverse().forEach(rowNum => sheet.deleteRow(rowNum));
    return ContentService.createTextOutput(`已成功删除${targetRows.length}条匹配记录。`);
  }

  // 非delete操作时的默认返回
  return ContentService.createTextOutput("无效操作类型");
}

核心修改说明:

  • 新增提取Name参数,通过解构赋值统一变量名
  • 读取全表数据后逐行校验,同时匹配姓名和邮箱两个条件
  • 收集所有匹配行的行号,倒序删除(解决删除行后行号错乱的问题)
  • 更新返回提示,明确双条件匹配的结果

前端JavaScript代码(无需修改)

当前前端代码已经会自动将URL中所有查询参数(包括Name、Email、sample)传递给Web Apps,因此保持原样即可:

if (performance.navigation.type != performance.navigation.TYPE_RELOAD) {
  const scriptURL = 'https://script.google.com/macros/s/###/exec'; // 替换为你的Web Apps地址

  const queryParameters = [...new URLSearchParams(window.location.search).entries()];
  const data = new FormData();
  queryParameters.forEach(e => data.append(...e));
  fetch(scriptURL, { method: 'POST', body: data })
    .then(response => console.log('Success!', response))
    .catch(error => console.error('Error!', error.message))
}

注意事项:

  • 请根据你的Google Sheet实际列布局,调整代码中姓名和邮箱对应的数组索引(比如姓名在第3列就用currentRow[2],数组索引从0开始)
  • 部署Web Apps时,确保权限设置符合你的需求(如需公开访问可设为"任何人,甚至匿名用户")
  • 测试时直接访问目标URL即可触发删除操作,可通过浏览器控制台和Sheet内容验证结果

内容的提问来源于stack exchange,提问作者jparko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 22:43:13