实现Google Sheets单元格点击自动弹出评论框用于操作记录
实现Google Sheets L列的自动评论框与时间戳记录功能
需求1:点击L列单元格自动弹出记录窗口
需求2:在L列生成带日期时间戳的操作记录(如MESSAGE SENT、CALLED)
以下是通过Google Apps Script实现的完整方案:
步骤1:添加脚本代码
- 打开你的Google Sheets文档
- 点击顶部菜单「扩展程序」→「Apps 脚本」
- 删除编辑器中的默认代码,粘贴以下代码:
// 选中单元格变化时触发,检查是否为L列并弹出记录窗口 function onSelectionChange(e) { const range = e.range; // 仅对L列(第12列)的非表头行生效(这里假设第1行是表头) if (range.columnStart === 12 && range.rowStart > 1) { SpreadsheetApp.getUi().showModalDialog( HtmlService.createHtmlOutput(` <div style="padding: 15px;"> <label style="display: block; margin-bottom: 8px;">操作类型:</label> <select id="actionType" style="width: 300px; padding: 8px; margin-bottom: 10px;"> <option value="MESSAGE SENT">MESSAGE SENT</option> <option value="CALLED">CALLED</option> </select> <label style="display: block; margin-bottom: 8px;">备忘内容:</label> <textarea id="commentInput" style="width: 300px; height: 80px; padding: 8px;" placeholder="输入详细信息..."></textarea> <button onclick="submitComment()" style="margin-top: 10px; padding: 8px 16px; background: #1a73e8; color: white; border: none; border-radius: 4px;">保存记录</button> </div> <script> function submitComment() { const actionType = document.getElementById('actionType').value; const input = document.getElementById('commentInput').value; google.script.run.addRecordWithTimestamp(${range.rowStart}, actionType, input); google.script.host.close(); } </script> `), '添加操作记录' ); } } // 生成带时间戳的记录并写入L列单元格 function addRecordWithTimestamp(row, actionType, note) { const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const cell = sheet.getRange(row, 12); // 生成格式为「YYYY/MM/DD HH:mm」的时间戳 const timestamp = new Date().toLocaleString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false }); // 构建记录内容 let record = `[${timestamp}] ${actionType}`; if (note.trim()) { record += `: ${note}`; } // 追加到单元格(已有内容则换行) const existingContent = cell.getValue() || ''; const newContent = existingContent ? `${existingContent}\n${record}` : record; cell.setValue(newContent); }
- 点击左上角保存按钮,给项目命名(比如「L列操作记录工具」)
步骤2:使用说明
- 返回Google Sheets表格,选中L列任意非表头单元格,会自动弹出记录窗口
- 选择操作类型(MESSAGE SENT/CALLED),输入备忘内容,点击「保存记录」
- L列单元格会自动生成带时间戳的记录,格式示例:
[10/05/2024 14:30] MESSAGE SENT: 告知客户订单已发货 [10/05/2024 15:15] CALLED: 跟进售后问题,客户表示满意
自定义调整
- 添加更多操作类型:在脚本的HTML部分,给
<select>标签添加新的<option>即可,比如:<option value="FOLLOW UP">FOLLOW UP</option> - 修改时间戳格式:调整
addRecordWithTimestamp函数中toLocaleString的参数,比如改成中文格式:const timestamp = new Date().toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }); - 开启评论同步:如果需要同时给单元格添加评论,取消
addRecordWithTimestamp函数中最后一行的注释:// cell.setComment(record);
内容的提问来源于stack exchange,提问作者Rob Sands
相关产品推荐
相关产品推荐

