每日自动将表格指定列及单个单元格数据复制到另一表格新列
调整后的Google Apps Script脚本
function copyDailyData() { // 替换为你的源表格和目标表格ID const sourceSpreadsheetId = '你的源表格ID'; const destinationSpreadsheetId = '你的目标表格ID'; // 1. 指定源表格的特定标签页(替换成实际的标签页名称) const sourceSheet = SpreadsheetApp.openById(sourceSpreadsheetId).getSheetByName('源标签页名称'); // 替换为你要复制的指定列(比如B列)和单个单元格(比如C1) const sourceColumnRange = sourceSheet.getRange('B:B'); const sourceCell = sourceSheet.getRange('C1'); // 获取源数据:过滤掉列中的空行,只保留有内容的行 const sourceColumnValues = sourceColumnRange.getValues().filter(row => row[0] !== ''); const sourceCellValue = sourceCell.getValue(); // 2. 处理目标表格 const destinationSheet = SpreadsheetApp.openById(destinationSpreadsheetId).getActiveSheet(); const nextColumn = destinationSheet.getLastColumn() + 1; // 写入单个单元格数据到目标表格的A行对应列(即第1行,nextColumn列) destinationSheet.getRange(1, nextColumn).setValue(sourceCellValue); // 写入指定列数据到目标表格的B行开始(即第2行,nextColumn列) if (sourceColumnValues.length > 0) { destinationSheet.getRange(2, nextColumn, sourceColumnValues.length, 1).setValues(sourceColumnValues); } }
关键修改说明
- 指定源表格的非第一个标签页:把原脚本里的
getActiveSheet()改成getSheetByName('源标签页名称'),替换引号里的内容为你实际的源标签页名称即可。 - 添加单个单元格复制逻辑:新增了获取指定单元格值的代码
sourceCell.getValue(),并将其写入目标表格对应新增列的第1行(也就是需求里的A行)。 - 优化数据写入逻辑:
- 过滤了源列中的空行,避免复制大量无效空值
- 直接找到目标表格的最后一列,下一列作为新增列写入,去掉了原脚本里多余的插入/删除列操作,逻辑更简洁
- 指定列数据从目标表格的第2行(B行)开始写入,符合需求要求
如何设置每日自动运行
- 打开脚本编辑器(点击「扩展程序」→「Apps 脚本」)
- 点击左侧菜单栏的「触发器」图标(时钟形状)
- 点击「添加触发器」,设置以下选项:
- 选择要运行的函数:
copyDailyData - 选择事件源:「时间驱动」
- 选择时间类型:「日计时器」
- 选择具体运行时间:根据你的需求设置即可
- 选择要运行的函数:
内容的提问来源于stack exchange,提问作者Louis Goffinet
相关产品推荐
相关产品推荐

