Google Script修改需求:默认将行复制到logs工作表
Got it, let's tweak this code so it always copies to the logs sheet instead of relying on column 10. Here's what you need to change:
Key Changes to Make:
- Remove the line that reads the target sheet name from column 10: This is the part that pulls the sheet name from row 10, which we don't need anymore.
- Hardcode the target sheet name as 'logs': Directly set the sheet we want to copy to, no more dynamic reading.
- Keep the sheet existence check: Optional but good practice to avoid errors if the
logssheet gets deleted accidentally.
Modified Code:
function onEdit(e) { var ss = e.source; var s = ss.getActiveSheet(); var r = e.range; // Configuration columns var actionCol = 9; // Column where "Yes" is entered // Removed nameCol since we don't need to read from column 10 anymore // Get the row and column of the active cell var rowIndex = r.getRowIndex(); var colIndex = r.getColumnIndex(); // Number of columns to copy (exclude action/status column) var colNumber = s.getLastColumn() - 1; // Trigger only when action column is set to "Yes" if (e.value == "Yes" && colIndex == actionCol) { // Hardcode target sheet name to 'logs' var targetSheetName = 'logs'; var targetSheet = ss.getSheetByName(targetSheetName); // Check if logs sheet exists before proceeding if (targetSheet) { // Define target range (next empty row in logs sheet) var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, colNumber); // Define source range (the edited row, excluding action column) var sourceRange = s.getRange(rowIndex, 1, 1, colNumber); // Copy the row to logs sheet sourceRange.copyTo(targetRange); // Mark the action column as "Logged" r.setValue("Logged"); } else { // Optional: Alert if logs sheet is missing (helps with debugging) SpreadsheetApp.getUi().alert('Error: The "logs" sheet does not exist in this spreadsheet!'); } } }
Quick Breakdown of the Adjustments:
- I removed the
nameColvariable and the line that fetched the sheet name from column 10—this was the core part tying the target sheet to that column's value. - Replaced it with a fixed
targetSheetName = 'logs'to always target that sheet. - Added an optional alert to notify you if the
logssheet goes missing, so you don't wonder why copies aren't happening. - Cleaned up redundant variables and comments to make the code easier to follow.
If you actually wanted to copy to the external spreadsheet (the one with the ID in your original code) instead of the current spreadsheet, just let me know and I can adjust the code for that!
内容的提问来源于stack exchange,提问作者R B
相关产品推荐
相关产品推荐

