javascript - 将具有更新时间戳的行移动到另一个 Google 表格
问题描述
我正在使用两个脚本来实现我的目标。当对字段进行更新时,第一个脚本会在相邻列中插入时间戳。这很好用。下一个脚本监视时间戳列,当时间戳更改时,将整行复制到“最近更新”表中。然后,我将使用 Awesome Table 插件为所有最近的更新创建新闻提要。
当时间戳列为空白并进行编辑时,时间戳将适当地输入到时间戳列中。第二个脚本将其拾取并将其裁剪到我的“最近更新”表中......
...但是如果进行了先前的更新并且时间戳字段已经存在,则脚本运行没有错误,但不会将新行复制到“最近更新”。每次时间戳字段更改时,如何让行粘贴?
/**
* @file Copy row to new cell when date value changes
* {@link https://support.google.com/docs/thread/13191603}
*/
/**
* Runs the snippet.
* Please, register this function for EDIT event
* once from the owner of the Spreadsheet
*
* @param {GoogleAppsScript.Events.SheetsOnEdit} e
*/
function CopyUpdates(e) {
if (!e) return;
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var currentRange = currentSheet.getActiveRange();
var currentRow = currentRange.getRow();
if (
e.value &&
currentSheet.getName() == "Open Actions - Cutover Punchlist" , "Open Actions - FSA Interfaces" , "Open Actions - General" &&
currentRow > 2 &&
currentRange.getColumn() == 9
) {
var dataRange = currentSheet.getRange(currentRow + ':' + currentRow);
var destinationSheet = currentSheet.getParent().getSheetByName("RecentUpdates");
var destinationRow = destinationSheet.getLastRow() + 1;
dataRange.copyTo(destinationSheet.getRange(destinationRow, 1), {
contentsOnly: true
});
}
}
解决方案
该脚本在测试后已经正常工作。也许有些东西会干扰您的触发器,这可能是由第一个功能引起的。因此,您将需要合并它们。
我将其重命名为onEdit(e)
。我合并了它们,因为它们实际上是 的子集onEdit(e)
,只是具有不同的条件。将它们合并到同一个函数下应该没问题。
代码:
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
function onEdit(e) {
if (!e) return;
var currentRange = currentSheet.getActiveRange();
var currentRow = currentRange.getRow();
if ( e.value &&
currentSheet.getName() == "Open Actions - Cutover Punchlist" , "Open Actions - FSA Interfaces" , "Open Actions - General" &&
currentRow > 2 ) {
if (currentRange.getColumn() == 8) { // adjacent column (first function conversion, if H column is edited)
var adjacentCell = currentSheet.getRange('H' + currentRow);
var timestampCell = adjacentCell.offset(0, 1);
timestampCell.setValue(new Date());
// since H is edited, timestamp column is updated
// so we copy (regardless if the old value is blank or a timestamp)
copyUpdates(currentRow);
}
if (currentRange.getColumn() == 9) { // timestamp column (second function conversion, if I column is edited)
// edited timestamp manually, copy
copyUpdates(currentRow);
}
}
}
function copyUpdates(currentRow) {
var dataRange = currentSheet.getRange(currentRow + ':' + currentRow);
var destinationSheet = currentSheet.getParent().getSheetByName("RecentUpdates");
var destinationRow = destinationSheet.getLastRow() + 1;
dataRange.copyTo(destinationSheet.getRange(destinationRow, 1), {
contentsOnly: true
});
}
样本数据:
样品测试:
1. Wrote "add timestamp" to "H3" (Should trigger your first function)
2. Wrote "add timestamp" to "H4" (Should trigger your first function)
3. Edited "H4" to "change timestamp" (Should trigger your first function)
4. Edited "I4" to "1/22/2021" (Should trigger your second function)
样本数据结果:
最近更新结果:
推荐阅读
- css - 如何在 jodit-react 中更改工具栏背景颜色
- python - 在混合列表中查找最高值,然后返回项目名称和项目名称 + VAT%
- servo - 电机伺服MG996R旋转角度
- c++ - 水平/垂直/对角翻转 4x4 位网格
- python - 在熊猫数据框中将字符串月-年转换为日期时间
- reactjs - 如何从 apache 子文件夹构建 React 应用程序?
- reactjs - 当反应方向发生变化时,使用参考更新元素的 offsetTop 和高度
- typescript - 将接口限制为联合类型中的特定键
- sql - 每年按年分组,年+9,得到计数的总和
- rxjs - 当您无法控制发布者时,是否可以/建议使用反应式编程