"function consolidateTrades() {
  let outputSheet; // Declare outputSheet outside of the try block
  try {
    const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = spreadsheet.getSheetByName('Trade Log_Raw');
    if (!sheet) {
      SpreadsheetApp.getUi().alert('Sheet "Trade Log_Raw" not found');
      return;
    }
    
    // Get headers from the first row (columns A to K)
    const headersRange = sheet.getRange(1, 1, 1, 11); // Limit to columns A (1) to K (11)
    const headers = headersRange.getValues()[0];

    // Get data starting from row 3 (columns A to K)
    const dataRange = sheet.getRange(3, 1, sheet.getLastRow() - 2, 11); // Limit to columns A (1) to K (11)
    const data = dataRange.getValues();
    if (data.length === 0) {
      SpreadsheetApp.getUi().alert('No data found to consolidate.');
      return;
    }
    
    const consolidatedData = [];
    
    // Create a map to store consolidated results
    const tradeMap = new Map();
    
    // Iterate over each row of data
    data.forEach(row => {
      const key = [row[0], row[1], row[2], row[3], row[5], row[8], row[9], row[10]].join('|');
      
      if (!tradeMap.has(key)) {
        tradeMap.set(key, {
          type: row[0],
          date: row[1],
          security: row[2],
          action: row[3],
          quantity: 0,
          price: row[5],
          fees: row[6],
          total: 0,
          assetClass: row[8],
          broker: row[9],
          user: row[10]
        });
      }
      
      const trade = tradeMap.get(key);
      trade.quantity += row[4];
      trade.total += row[7];
    });
    
    // Convert map to array
    tradeMap.forEach(trade => {
      consolidatedData.push([
        trade.type, trade.date, trade.security, trade.action, trade.quantity,
        trade.price, trade.fees, trade.total, trade.assetClass, trade.broker, trade.user
      ]);
    });
    
    // Output to the output sheet, without touching rows 1 and 2
    outputSheet = spreadsheet.getSheetByName('Trade Log');
    if (!outputSheet) {
      outputSheet = spreadsheet.insertSheet('Trade Log');
    } else {
      // Clear contents from row 3 onwards (do not clear formatting), limit to columns A to K
      const lastRow = outputSheet.getLastRow();
      if (lastRow > 2) {
        outputSheet.getRange(3, 1, lastRow - 2, 11).clearContent(); // Limit to columns A (1) to K (11)
      }
    }

    // Check if headers exist in output sheet, if not, write them
    const existingHeaders = outputSheet.getRange(1, 1, 1, 11).getValues()[0]; // Limit to columns A (1) to K (11)
    const headersExist = existingHeaders.every(cell => cell !== ""); // Check if all cells are non-empty

    if (!headersExist) {
      outputSheet.getRange(1, 1, 1, 11).setValues([headers]); // Limit to columns A (1) to K (11)
    } else {
      // Optionally, clear the first row to ensure headers are correct
      outputSheet.getRange(1, 1, 1, 11).clearContent(); // Limit to columns A (1) to K (11)
      outputSheet.getRange(1, 1, 1, 11).setValues([headers]); // Limit to columns A (1) to K (11)
    }

    // Set the consolidated data starting from row 3
    if (consolidatedData.length > 0) {
      outputSheet.getRange(3, 1, consolidatedData.length, 11).setValues(consolidatedData); // Limit to columns A (1) to K (11)
    }

    // Do not touch formatting, as per your request
    // Removed the code that re-applies formatting

  } catch (e) {
    SpreadsheetApp.getUi().alert('An error occurred: ' + e.message);
  }

  // Record the last update date and time within column K or earlier
  if (outputSheet) { // Check if outputSheet is defined
    let now = new Date();
    let formattedNow = Utilities.formatDate(now, "Asia/Kolkata", "dd-MMM-yyyy HH:mm:ss");
    outputSheet.getRange('K1').setValue(formattedNow); // Write to cell K1
  }
}"

isko explain karo