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

使用Google Apps Script修改Google Sheet单元格实现居民缴费系统改密功能

居民缴费系统修改密码功能实现方案

一、修改前端WebAppLogin.html代码

首先补充改密交互的校验和参数传递逻辑,你需要先确保登录成功后把接口返回的用户名存在全局变量(比如在checkLogin的成功回调中加window.currentUser = 登录返回的用户名),然后替换原有changePassword函数:

// 以下input的id请和你实际页面的输入框id保持一致
function changePassword(){
  const oldPwd = document.getElementById('oldPwd').value.trim();
  const newPwd = document.getElementById('newPwd').value.trim();
  const confirmPwd = document.getElementById('confirmPwd').value.trim();

  // 前端基础校验
  if(!oldPwd || !newPwd || !confirmPwd) {
    alert('请填写所有密码字段');
    return;
  }
  if(newPwd !== confirmPwd) {
    alert('两次输入的新密码不一致');
    return;
  }
  if(newPwd.length < 6) {
    alert('新密码长度不能少于6位');
    return;
  }

  if (confirm("确认要修改密码吗?")) {
    google.script.run
      .withSuccessHandler(res => {
        if(res.success) {
          alert('密码修改成功,请重新登录');
          window.location.reload();
        } else {
          alert('修改失败:' + res.msg);
        }
      })
      .withFailureHandler(err => {
        alert('系统错误:' + err);
      })
      .updatePassword(window.currentUser, oldPwd, newPwd);
  }
}

二、后端Code.gs新增改密处理函数

直接在现有Code.gs代码末尾追加以下函数即可:

function updatePassword(username, oldPwd, newPwd) {
  try {
    const ss = SpreadsheetApp.openByUrl(url);
    const userSheet = ss.getSheetByName("USERNAMES");
    const lastRow = userSheet.getLastRow();
    // 遍历匹配用户
    for(let i = 2; i <= lastRow; i++) {
      const sheetUsername = userSheet.getRange(i, 1).getValue().toUpperCase();
      const sheetPwd = userSheet.getRange(i, 7).getValue().toString();
      // 用户名和旧密码匹配则更新
      if(sheetUsername === username.toUpperCase() && sheetPwd === oldPwd) {
        userSheet.getRange(i, 7).setValue(newPwd);
        return {success: true, msg: '修改成功'};
      }
    }
    return {success: false, msg: '旧密码错误,请重试'};
  } catch (e) {
    return {success: false, msg: e.message};
  }
}

三、使用注意事项

  • 确认你的USERNAMES表中密码存储列是第7列(G列),如果不是请修改代码中getRange(i,7)的第二个参数为你实际的列序号
  • 代码修改完成后部署Web App时要发布新版本,否则新代码不会生效
  • 后续优化建议:不要明文存储密码,可使用Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, 密码字符串)做哈希加密后再存储,校验时也将输入密码哈希后再对比,提升安全性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 10:06:03