链接到我的工作表:https://docs.google.com/spreadsheets/d/1gkWCU0fHotORi-urOfuHjGGiqrnYcdtZQ9bZhm7XLIk/edit?usp=sharing
问题: 在删除日期或取消选中复选框时如何解决编辑问题,而又不影响其他列。 (请查看图片)
“功能”按钮的工作方式:
菜单面板:功能> 插入新列
通过操作脚本,可以插入任意多列。
我的AutoTimeStamp脚本如何工作: 选中复选框(C列)后,日期自动应用于D列。
“复制”脚本的工作方式: 激活插入新列后,它将从左到右复制C列和D列。还将TimeStamp功能应用于每个新创建的列。
这是插入新列功能:
var ss = SpreadsheetApp.getActive();
function onOpen() {
var menu = [{name:"Insert New Columns", functionName:"addColumn"}];
ss.addMenu("Functions", menu);
}
function addColumn() {
var sh = ss.getActiveSheet(), lCol = sh.getLastColumn();
var lRow = sh.getLastRow(), range = sh.getRange(1,lCol-1,lRow,2);
sh.insertColumnsAfter(lCol,10);
var newRange = sh.getRange(1,lCol+1,lRow,10);
var columnWidths = SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS
range.copyTo(newRange);
range.copyTo(newRange,columnWidths,false);
newRange.setFormulas(newRange.getFormulas());
}
这是 AutoTimeStamp 脚本:
var COLUMNTOCHECK = 3;
var DATETIMELOCATION = [0, 1];
var SHEETNAME = 'Training Dash'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if( selectedCell.getColumn() >= COLUMNTOCHECK) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
这是一些宏旁边的 Copy 脚本。
function Copy() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getActiveSheet().insertColumnsAfter(spreadsheet.getActiveRange().getLastColumn(), 2);
spreadsheet.getActiveRange().offset(0, spreadsheet.getActiveRange().getNumColumns(), spreadsheet.getActiveRange().getNumRows(), 2).activate();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn(), sheet.getMaxRows(), 1).activate();
sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn() - 2, sheet.getMaxRows(), 2).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
function UntitledMacro() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getActiveSheet().insertColumnsAfter(spreadsheet.getActiveRange().getLastColumn(), 2);
spreadsheet.getActiveRange().offset(0, spreadsheet.getActiveRange().getNumColumns(), spreadsheet.getActiveRange().getNumRows(), 2).activate();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn() - 2, sheet.getMaxRows(), 2).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn(), sheet.getMaxRows(), 2).activate();
sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn() - 2, sheet.getMaxRows(), 2).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS, false);
};
function UntitledMacro1() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn(), sheet.getMaxRows(), 2).activate();
spreadsheet.getActiveSheet().insertColumnsAfter(spreadsheet.getActiveRange().getLastColumn(), 2);
spreadsheet.getActiveRange().offset(0, spreadsheet.getActiveRange().getNumColumns(), spreadsheet.getActiveRange().getNumRows(), 2).activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet15'), true);
spreadsheet.getCurrentCell().offset(-2, 0, 17, 2).activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Training Dash'), true);
spreadsheet.getCurrentCell().activate();
spreadsheet.getRange('Sheet15!A1:B17').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn(), sheet.getMaxRows(), 2).activate();
sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn() - 2, sheet.getMaxRows(), 2).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS, false);
};
function UntitledMacro2() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('E:N').activate();
spreadsheet.getActiveSheet().insertColumnsBefore(spreadsheet.getActiveRange().getColumn(), 10);
spreadsheet.getActiveRange().offset(0, 0, spreadsheet.getActiveRange().getNumRows(), 10).activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet15'), true);
spreadsheet.getRange('A1:B17').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Training Dash'), true);
spreadsheet.getRange('E1:N1').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('Sheet15!A1:B17').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
spreadsheet.getRange('E:N').activate();
spreadsheet.getRange('O1:P16').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS, false);
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet15'), true);
spreadsheet.getActiveSheet().hideSheet();
};
答案 0 :(得分:1)
问题出在OnEdit(e)中的这一行代码
`if( selectedCell.getColumn() >= COLUMNTOCHECK) { `
这仅试图确定仅,而不考虑所选单元格的列号是否大于或等于3。不考虑该列包含复选框还是日期。因此,删除日期后,OnEdit会启动,并评估该列大于3,因此将日期戳放在所选单元格附近的单元格中。
解决方案是检查所选单元格的列是奇数还是偶数。包含复选框的列号为奇数,包含日期戳的列号为偶数。因此,如果列号为奇数,则OnEdit应该照常进行,但是如果列号为偶数,则OnEdit应该什么也不做。
使用模量进行奇/偶测试。请参阅this extensive discussion。
用此行替换问题代码:
` if (selectedCell.getColumn() >= COLUMNTOCHECK && selectedCell.getColumn() % 2 == 1) {`
区别是:
&&
-表示“与”-该代码现在正在评估两个结果
selectedCell.getColumn() % 2 == 1
-结果1(一)表示一个奇数。