"function updateOpenPortfolio() {
  // Start time tracking
  const startTime = new Date();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Trade Log");
  if (!sheet) {
    Logger.log("Trade Log sheet not found. Please check the sheet name.");
    return;
  }

  var portfolioSheet = ss.getSheetByName("Portfolio");
  if (!portfolioSheet) {
    portfolioSheet = ss.insertSheet("Portfolio");
    Logger.log(`Created new sheet: "Portfolio"`);
  }

  // Set headers if they are not already set
  var headerRange = portfolioSheet.getRange('A3:H3');
  var currentHeaders = headerRange.getValues()[0];
  var newHeaders = ["SN.", "Ticker", "Type", "# Shares", "Avg. Buy @", "Asset Class", "Broker", "User"];

  // Only set headers if they are different
  if (JSON.stringify(currentHeaders) !== JSON.stringify(newHeaders)) {
    headerRange.setValues([newHeaders]);
    Logger.log(`Set Portfolio headers to: ${newHeaders.join(", ")}`);
  }

  // Clear existing data in columns A to H starting from row 4
  var lastRow = portfolioSheet.getLastRow();
  if (lastRow > 3) {
    portfolioSheet.getRange(4, 1, lastRow - 3, 8).clearContent(); // Clear only columns A to H
    Logger.log(`Cleared existing data in "Portfolio" from row 4`);
  }

  // Batch read data from the Trade Log
  var dataRange = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
  var data = dataRange.getValues();

  if (data.length === 0) {
    Logger.log("No data found in Trade Log sheet.");
    return;
  }

  // Process header row
  var headers = data[0].map(header => header.toString().trim().toLowerCase());
  var expectedHeaders = {
    "type": ["type"],
    "date": ["date"],
    "security": ["security"],
    "action": ["action"],
    "quantity": ["quantity"],
    "@price": ["@price", "price"],
    "fees": ["fees"],
    "asset class": ["asset class"],
    "broker": ["broker"],
    "user": ["user"]
  };

  var headerIndices = {};
  var missingHeaders = [];

  for (var key in expectedHeaders) {
    var found = false;
    for (var i = 0; i < expectedHeaders[key].length; i++) {
      var possibleHeader = expectedHeaders[key][i].toLowerCase();
      var idx = headers.indexOf(possibleHeader);
      if (idx !== -1) {
        headerIndices[key] = idx;
        found = true;
        break;
      }
    }
    if (!found) {
      missingHeaders.push(key);
    }
  }

  if (missingHeaders.length > 0) {
    Logger.log("Missing Headers: " + missingHeaders.join(", "));
    throw new Error("One or more required columns are missing in Trade Log sheet.");
  }

  var positionsMap = new Map();

  function generateTrade(quantity, price, fees, action, assetClass, broker, user, date, tradeType) {
    return {
      shares: quantity,
      price: price,
      fees: fees,
      action: action,
      assetClass: assetClass,
      broker: broker,
      user: user,
      date: date,
      type: tradeType
    };
  }

  for (var i = 1; i < data.length; i++) {
    var row = data[i];

    // Check if the row is empty
    if (row.every(cell => cell === "")) {
      continue; // Skip empty rows
    }

    var type = (row[headerIndices["type"]] || "").toString().trim().toLowerCase();
    var dateCell = row[headerIndices["date"]];
    var date = dateCell instanceof Date ? dateCell : new Date(dateCell);
    if (isNaN(date.getTime())) {
      Logger.log(`Invalid date in row ${i + 2}: ${dateCell}`);
      continue;
    }
    var action = (row[headerIndices["action"]] || "").toString().trim().toLowerCase();
    var ticker = (row[headerIndices["security"]] || "").toString().trim();
    var quantity = parseFloat(row[headerIndices["quantity"]]) || 0;
    var price = parseFloat(row[headerIndices["@price"]]) || 0.0;
    var fees = parseFloat(row[headerIndices["fees"]]) || 0.0;
    var assetClass = (row[headerIndices["asset class"]] || "Unknown").toString().trim();
    var broker = (row[headerIndices["broker"]] || "Unknown").toString().trim();
    var user = (row[headerIndices["user"]] || "Unknown").toString().trim();

    var tradeType = "";

    if (type === 'delivery') {
      if (action === 'buy') {
        tradeType = 'Long';
        let key = `${ticker}|${broker}|${user}|${assetClass}|${tradeType}`;
        let trade = generateTrade(quantity, price, fees, action, assetClass, broker, user, date, tradeType);
        let activeTrades = positionsMap.get(key);
        if (!activeTrades) {
          positionsMap.set(key, [trade]);
        } else {
          activeTrades.push(trade);
        }
      } else if (action === 'sell') {
        tradeType = 'Long';
        let key = `${ticker}|${broker}|${user}|${assetClass}|${tradeType}`;
        let activeTrades = positionsMap.get(key);
        if (activeTrades) {
          let sharesToProcess = quantity;
          activeTrades.sort((a, b) => a.date - b.date); // FIFO
          let iTrade = 0;
          while (sharesToProcess > 0 && iTrade < activeTrades.length) {
            let trade = activeTrades[iTrade];
            if (trade.shares <= sharesToProcess) {
              sharesToProcess -= trade.shares;
              activeTrades.splice(iTrade, 1);
            } else {
              trade.shares -= sharesToProcess;
              sharesToProcess = 0;
            }
          }
          if (activeTrades.length == 0) {
            positionsMap.delete(key);
          }
        } else {
          Logger.log(`Warning: No positions found for key ${key} when processing sell action.`);
        }
      } else if (action === 'sellshort') {
        tradeType = 'Short';
        let key = `${ticker}|${broker}|${user}|${assetClass}|${tradeType}`;
        let trade = generateTrade(quantity, price, fees, action, assetClass, broker, user, date, tradeType);
        let activeTrades = positionsMap.get(key);
        if (!activeTrades) {
          positionsMap.set(key, [trade]);
        } else {
          activeTrades.push(trade);
        }
      } else if (action === 'buytocover') {
        tradeType = 'Short';
        let key = `${ticker}|${broker}|${user}|${assetClass}|${tradeType}`;
        let activeTrades = positionsMap.get(key);
        if (activeTrades) {
          let sharesToProcess = quantity;
          activeTrades.sort((a, b) => a.date - b.date); // FIFO
          let iTrade = 0;
          while (sharesToProcess > 0 && iTrade < activeTrades.length) {
            let trade = activeTrades[iTrade];
            if (trade.shares <= sharesToProcess) {
              sharesToProcess -= trade.shares;
              activeTrades.splice(iTrade, 1);
            } else {
              trade.shares -= sharesToProcess;
              sharesToProcess = 0;
            }
          }
          if (activeTrades.length == 0) {
            positionsMap.delete(key);
          }
        } else {
          Logger.log(`Warning: No positions found for key ${key} when processing buytocover action.`);
        }
      }
    } else if (type === 'corp. action' && action === 'split') {
      // Process stock splits
      let splitRatioStr = (row[headerIndices["quantity"]] || "").toString().trim();
      let ratio = 1;
      if (splitRatioStr.includes(":")) {
        let ratioParts = splitRatioStr.split(':');
        let numerator = parseFloat(ratioParts[0]);
        let denominator = parseFloat(ratioParts[1]);
        ratio = numerator / denominator;
      } else {
        ratio = parseFloat(splitRatioStr);
      }
      positionsMap.forEach((value, pKey) => {
        if (pKey.startsWith(`${ticker}|`)) {
          value.forEach(trade => {
            trade.shares *= ratio;
            trade.price /= ratio;
          });
        }
      });
    } else if (type === 'my action') {
      if (action === 'transferfrom') {
        // Handle transfer from one broker to another
        tradeType = 'Long'; // Assuming transfer applies to Long positions
        let key = `${ticker}|${broker}|${user}|${assetClass}|${tradeType}`;
        let nextRow = data[i + 1];
        if (nextRow) {
          let newBroker = (nextRow[headerIndices["broker"]] || "Unknown").toString().trim();
          let newKey = `${ticker}|${newBroker}|${user}|${assetClass}|${tradeType}`;
          let activeTrades = positionsMap.get(key);
          if (activeTrades) {
            let sharesToTransfer = quantity;
            let totalAvailableShares = activeTrades.reduce((sum, trade) => sum + trade.shares, 0);
            if (sharesToTransfer > totalAvailableShares) {
              Logger.log(`Not enough shares to transfer for ${key}.`);
              continue;
            }
            let transferredTrades = [];
            activeTrades.sort((a, b) => a.date - b.date); // FIFO
            while (sharesToTransfer > 0 && activeTrades.length > 0) {
              let trade = activeTrades[0];
              if (trade.shares <= sharesToTransfer) {
                sharesToTransfer -= trade.shares;
                transferredTrades.push({ ...trade }); // Copy trade
                activeTrades.splice(0, 1);
              } else {
                let transferredTrade = { ...trade, shares: sharesToTransfer };
                trade.shares -= sharesToTransfer;
                sharesToTransfer = 0;
                transferredTrades.push(transferredTrade);
              }
            }
            if (activeTrades.length == 0) {
              positionsMap.delete(key);
            }
            // Add transferred trades to new broker
            let newActiveTrades = positionsMap.get(newKey);
            if (!newActiveTrades) {
              positionsMap.set(newKey, transferredTrades);
            } else {
              newActiveTrades.push(...transferredTrades);
            }
          } else {
            Logger.log(`No active trades found for key ${key} during transfer.`);
          }
        } else {
          Logger.log(`No next row found for transfer action at row ${i + 2}.`);
        }
      }
    }
  } // End of loop processing

  // Prepare data for output
  let returnArry = [];
  let sn = 1;
  let sortedKeys = Array.from(positionsMap.keys()).sort();
  sortedKeys.forEach(key => {
    let value = positionsMap.get(key);
    let totalShares = 0;
    let totalCost = 0;
    let totalFees = 0;
    let avgPrice = 0;
    let [ticker, currentBroker, currentUser, currentAssetClass, longOrShort] = key.split('|');
    value.forEach(trade => {
      totalShares += trade.shares;
      totalCost += trade.shares * trade.price;
      totalFees += trade.fees;
    });
    avgPrice = totalShares > 0 ? (totalCost + totalFees) / totalShares : 0;

    returnArry.push([
      sn++,
      ticker,
      longOrShort,
      Number(totalShares.toFixed(4)),
      avgPrice.toFixed(2),
      currentAssetClass,
      currentBroker,
      currentUser
    ]);
  });

  // Output to "Portfolio" sheet
  if (returnArry.length > 0) {
    portfolioSheet.getRange(4, 1, returnArry.length, returnArry[0].length).setValues(returnArry);
    portfolioSheet.getRange(4, 4, returnArry.length, 2).setNumberFormat('#,##0.00');
    Logger.log(`Portfolio updated with ${returnArry.length} records.`);
  } else {
    Logger.log("No open positions to display.");
  }

  // Record the last update date and time
  let now = new Date();
  let formattedNow = Utilities.formatDate(now, "Asia/Kolkata", "dd-MMM-yyyy HH:mm:ss");
  portfolioSheet.getRange('V1').setValue(formattedNow);

  Logger.log("Open portfolio updated successfully.");

  // End time tracking and calculate duration
  const endTime = new Date();
  const durationMs = endTime - startTime; // Duration in milliseconds
  const durationSeconds = (durationMs / 1000).toFixed(2); // Duration in seconds with two decimal places
  Logger.log(`Script execution time: ${durationSeconds} seconds.`);
}"

ye script esa portfolio kyu bana rahi h 

jese 

"SN.	Ticker	Type	# Shares	Avg. Buy @	Asset Class	Broker	User
17	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Long	0.00	0.00	 Equity 	 ICICI 	 YKM "

jb ki total buy quantity sold ho chuki h issse to portfolio me hona hi nhi tha 

ye h uski trade log

"Type	Date	Security	Action	Quantity	@Price	Fees	Total ₹ (In/Out)	Asset Class	Broker	User
Delivery	29-May-2007	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	499.22	10.02	 ₹ -   	 ₹ 5,002.18 	 Equity 	 ICICI 	 YKM 
Delivery	28-Jan-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	2.86	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Feb-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.51	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Mar-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	2.65	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Apr-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	3.64	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	27-May-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	2.99	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Jun-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.58	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Jul-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.93	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Aug-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	3.37	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Sep-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.31	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	29-Oct-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	6.03	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Nov-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	3.72	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	29-Dec-2008	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	13.76	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Jan-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.33	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Feb-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.98	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Mar-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	4.93	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Apr-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	6.87	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-May-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.54	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Jun-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.62	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Jul-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.74	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Nov-2009	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	1.88	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	27-Jan-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.38	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Feb-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.51	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Mar-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.92	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	27-Apr-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	2.91	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-May-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	2.35	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Sep-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.55	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Nov-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	1.40	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Dec-2010	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	3.85	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Feb-2011	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	0.76	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Mar-2011	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	2.32	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-Apr-2011	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	3.75	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	26-May-2011	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	2.22	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	28-Jun-2011	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Buy	5.39	0.00	 ₹ -   	 ₹ -   	 Equity 	 ICICI 	 YKM 
Delivery	2-Jul-2011	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Sell	584.77	10.13	 ₹ -   	 ₹ 5,923.72 	 Equity 	 ICICI 	 YKM "

total buy quantity 584.77 total sell qty 584.77 to portfolio me anan hi nhi chahiye.

pr esa dikha raha h 

"SN.	Ticker	Type	# Shares	Avg. Buy @	Asset Class	Broker	User
17	MF:L AND T FLEXI BOND FUND - RETAIL PLAN DIVIDEND OPTION	Long	0.00	0.00	 Equity 	 ICICI 	 YKM "