google-sheetsgoogle-apps-scripttriggers

Why doesn't the last if part work in onedit?


Only the first if part is executed repeatedly and it does not go to the next if part. I made a few dropboxes on different sheets. If you select one from the dropbox, another dropbox that matches the value is imported and pasted. how should i solve it

var sheet = SpreadsheetApp.getActiveSheet();

var spreadsheet = SpreadsheetApp.getActive();

function onEdit(e){

    if (sheet.getRange('C15').getValue() == 'line'){
    //Omit details
    spreadsheet.getRange('dropboxs!B2:C3').copyTo(spreadsheet. getRange('C18'),SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    }

    if (sheet.getRange('C18').getValue() == 'c'){
    //Omit details
    spreadsheet.getRange('dropboxs!B2:C3').copyTo(spreadsheet. getRange('C21'),SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    }
}

Solution

  • try to treat it independently:

    var spreadsheet = SpreadsheetApp.getActive();
    
    // This function is triggered whenever an edit is made to the spreadsheet
    function onEdit(e) {
    var sheet = e.source.getActiveSheet();
      
    // Check if the value in cell C15 is 'line'
    if (sheet.getRange('C15').getValue() == 'line') {
        
    // Copy the range B2:C3 from the 'dropboxs' sheet to cell C18
    spreadsheet.getRange('dropboxs!B2:C3').copyTo(spreadsheet.getRange('C18'), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
        
    // Set a timed trigger to check the condition in cell C18 after the copy operation
    ScriptApp.newTrigger('checkC18Condition')
    .timeBased()
    .after(500)  // Wait for 0.5 seconds before running the check
    .create();
    }}
    
    // This function is triggered by the timed trigger created in onEdit
    function checkC18Condition() {
    
    // Ensure we're working with the active spreadsheet and sheet
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      
    // Check if the value in cell C18 is 'c'
    if (sheet.getRange('C18').getValue() == 'c') {
    
    // Copy the range B2:C3 from the 'dropboxs' sheet to cell C21
    spreadsheet.getRange('dropboxs!B2:C3').copyTo(spreadsheet.getRange('C21'), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    }}