求助修改Google Apps Script:按指定列映射迁移数据并删除原行
Fix for Your Row Migration Script
Got it, let's tweak your script to handle the specific column mapping and avoid copying the entire row. Here's the revised code that matches your requirements exactly:
function onEdit(event) { const ss = SpreadsheetApp.getActiveSpreadsheet(); const s = event.source.getActiveSheet(); const r = event.source.getActiveRange(); // Validate the edit context: Sheet2, column 8 (H), value is "Done" if (s.getName() === "Sheet2" && r.getColumn() === 8 && r.getValue() === "Done") { const row = r.getRow(); const targetSheet = ss.getSheetByName("URL Checklist Raw"); // Extract only the required columns, ordered to match the target mapping // Source: A → Target A; B → Target B; G → Target C; C → Target D const mappedValues = [ s.getRange(row, 1).getValue(), // Source A s.getRange(row, 2).getValue(), // Source B s.getRange(row, 7).getValue(), // Source G (maps to Target C) s.getRange(row, 3).getValue() // Source C (maps to Target D) ]; // Write the mapped values to the next empty row in the target sheet const nextTargetRow = targetSheet.getLastRow() + 1; targetSheet.getRange(nextTargetRow, 1, 1, mappedValues.length).setValues([mappedValues]); // Remove the completed row from Sheet2 s.deleteRow(row); } }
Key Changes Explained:
- Targeted Column Selection: Instead of copying the entire row, we explicitly grab only columns A, B, G, and C from the source row, ordered to fit your target mapping (A→A, B→B, G→C, C→D).
- Clean Value Writing: Using
setValues()ensures we only write the data we need, which won't interfere with any auto-fill rules on the target sheet (unlikemoveTo()which carries over row formatting and extra columns). - Simplified Logic: We kept the core trigger condition intact, so the script still runs only when you set column H in Sheet2 to "Done".
Just replace your original script with this version, and it should work exactly as you need!
内容的提问来源于stack exchange,提问作者Niño Regala
相关产品推荐
相关产品推荐

