"function queryPnLData() {

  // Start time tracking
  const startTime = new Date();

  const sheet = SpreadsheetApp.getActiveSpreadsheet();
  const outputSheet = sheet.getSheetByName("L/S/I Term_Filter") || sheet.insertSheet("L/S/I Term_Filter");

  // Clear data starting from row 3
  outputSheet.getRange(3, 1, outputSheet.getMaxRows() - 2, outputSheet.getMaxColumns()).clearContent();

  // Get input parameters from outputSheet
  const b1Date = outputSheet.getRange("B1").getValue();
  const d1Date = outputSheet.getRange("D1").getValue();
  let f1Value = outputSheet.getRange("F1").getValue().toString().trim();

  // Standardize the input value
  const validValues = {
    "long term": "Long Term",
    "short term": "Short Term",
    "intraday": "Intraday",
    "all": "All"
  };

  f1Value = validValues[f1Value.toLowerCase()] || f1Value;

  // Function to filter data based on date range
  function filterData(data, dateColumnIndex) {
    return data.filter(row => {
      const rowDate = new Date(row[dateColumnIndex]);
      return rowDate >= b1Date && rowDate <= d1Date;
    });
  }

  // Function to rearrange columns for output (moving Trade Date to column C)
  function rearrangeColumns(data, isIntraday) {
    return data.map(row => {
      if (isIntraday) {
        // For Intraday data: Move Trade Date from B to C
        return [
          row[0],  // Sl No.
          "",      // Empty column B
          row[1],  // Trade Date to column C
          ...row.slice(2)  // Rest of the columns
        ];
      } else {
        // For PnL data: Already in correct format
        return row;
      }
    });
  }

  // Function to get and process data from a sheet
  function getSheetData(sheetName, filterValue) {
    const dataSheet = sheet.getSheetByName(sheetName);
    if (!dataSheet) {
      throw new Error(`Sheet "${sheetName}" not found`);
    }

    const header = dataSheet.getRange("A4:W4").getValues()[0];
    const data = dataSheet.getRange("A5:W" + dataSheet.getLastRow()).getValues();

    // Set date column index based on sheet
    const dateColumnIndex = sheetName === "PnL" ? 2 : 1; // Column C for PnL, Column B for PnL_i

    let filteredData;

    if (sheetName === "PnL") {
      if (filterValue === "All") {
        // Filter data with term type 'Long Term' or 'Short Term'
        filteredData = filterData(data, dateColumnIndex).filter(row =>
          row[22] === "Long Term" || row[22] === "Short Term"
        );
      } else {
        // Filter data with term type equal to filterValue
        filteredData = filterData(data, dateColumnIndex).filter(row =>
          row[22] === filterValue
        );
      }
    } else if (sheetName === "PnL_i") {
      if (filterValue === "Intraday" || filterValue === "All") {
        // All data in PnL_i sheet is Intraday, so filter only by date
        filteredData = filterData(data, dateColumnIndex);
      } else {
        // For other filterValues, no data from PnL_i sheet
        filteredData = [];
      }
    }

    // Rearrange columns if it's intraday data
    const rearrangedData = rearrangeColumns(filteredData, sheetName === "PnL_i");
    const rearrangedHeader = rearrangeColumns([header], sheetName === "PnL_i")[0];

    return { header: rearrangedHeader, filteredData: rearrangedData };
  }

  // Function to make header row bold
  function makeHeaderBold(range) {
    range.setFontWeight('bold');
  }

  // Validate F1 value
  if (!Object.values(validValues).includes(f1Value)) {
    throw new Error(`Invalid F1 value: "${f1Value}". Must be 'Long Term', 'Short Term', 'Intraday', or 'All'`);
  }

  switch(f1Value) {
    case "Long Term":
    case "Short Term":
      const pnlData = getSheetData("PnL", f1Value);

      // Get number of columns for the data
      const pnlNumColumns = pnlData.header.length;

      // Output header at row 4
      outputSheet.getRange(4, 1, 1, pnlNumColumns).setValues([pnlData.header]);
      makeHeaderBold(outputSheet.getRange(4, 1, 1, pnlNumColumns));
      if (pnlData.filteredData.length > 0) {
        // Output data starting from row 5
        outputSheet.getRange(5, 1, pnlData.filteredData.length, pnlNumColumns)
          .setValues(pnlData.filteredData);
      } else {
        // Output message at row 5
        outputSheet.getRange(5, 1).setValue("No data found for the selected criteria.");
      }
      break;

    case "Intraday":
      const intradayData = getSheetData("PnL_i", f1Value);

      // Get number of columns for the data
      const intradayNumColumns = intradayData.header.length;

      // Output header at row 4
      outputSheet.getRange(4, 1, 1, intradayNumColumns).setValues([intradayData.header]);
      // Format the header
      let intradayHeaderRange = outputSheet.getRange(4, 1, 1, intradayNumColumns);
      intradayHeaderRange.setFontWeight('bold');
      intradayHeaderRange.setFontColor('white');
      intradayHeaderRange.setBackground('black');
      if (intradayData.filteredData.length > 0) {
        // Output data starting from row 5
        outputSheet.getRange(5, 1, intradayData.filteredData.length, intradayNumColumns)
          .setValues(intradayData.filteredData);
      } else {
        // Output message at row 5
        outputSheet.getRange(5, 1).setValue("No data found for the selected criteria.");
      }
      break;

    case "All":
      const pnlAllData = getSheetData("PnL", "All");
      const intradayAllData = getSheetData("PnL_i", "All");

      // Get number of columns for PnL and Intraday data
      const pnlAllNumColumns = pnlAllData.header.length;
      const intradayAllNumColumns = intradayAllData.header.length;

      // Output first table (Long/Short Term)
      outputSheet.getRange(4, 1, 1, pnlAllNumColumns).setValues([pnlAllData.header]);
      makeHeaderBold(outputSheet.getRange(4, 1, 1, pnlAllNumColumns));
      if (pnlAllData.filteredData.length > 0) {
        outputSheet.getRange(5, 1, pnlAllData.filteredData.length, pnlAllNumColumns)
          .setValues(pnlAllData.filteredData);
      } else {
        outputSheet.getRange(5, 1).setValue("No Long Term or Short Term data found for the selected criteria.");
      }

      // Calculate starting row for second table (leave 2 rows gap)
      let firstTableEndRow;
      if (pnlAllData.filteredData.length > 0) {
        firstTableEndRow = 4 + pnlAllData.filteredData.length;
      } else {
        // When there's no data, we output the message at row 5
        firstTableEndRow = 5;
      }

      // Leave 2 rows gap
      const secondTableStartRow = firstTableEndRow + 2;

      // Add a label for the second table
      outputSheet.getRange(secondTableStartRow, 1).setValue("Intraday Data:");
      outputSheet.getRange(secondTableStartRow, 1).setFontWeight('bold');
      outputSheet.getRange(secondTableStartRow, 1).setHorizontalAlignment('left');

      // Output second table (Intraday)
      const intradayHeaderRow = secondTableStartRow + 1;
      outputSheet.getRange(intradayHeaderRow, 1, 1, intradayAllNumColumns)
        .setValues([intradayAllData.header]);
      // Format the header
      let intradayHeaderRangeAll = outputSheet.getRange(intradayHeaderRow, 1, 1, intradayAllNumColumns);
      intradayHeaderRangeAll.setFontWeight('bold');
      intradayHeaderRangeAll.setFontColor('white');
      intradayHeaderRangeAll.setBackground('black');

      const intradayDataStartRow = intradayHeaderRow + 1;
      if (intradayAllData.filteredData.length > 0) {
        outputSheet.getRange(intradayDataStartRow, 1, intradayAllData.filteredData.length, intradayAllNumColumns)
          .setValues(intradayAllData.filteredData);
      } else {
        outputSheet.getRange(intradayDataStartRow, 1).setValue("No Intraday data found for the selected criteria.");
      }
      break;
  }

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

  Logger.log("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
  outputSheet.getRange('R1').setValue(`Execution time: ${durationSeconds} seconds`);
  Logger.log(`Script execution time: ${durationSeconds} seconds.`);
}"

ye code poora sahi h sb kuch minor changes krne h

1. output table column A se start horahi h wo column B se start ho. sirf table ko ek column aage khaskana h. shift krnah.

2. column A me ab Sl. Serial number hoga

3. jb bhi cell F1 me "All" select hota h tb dono table aati h jesa ki tumhe pata h to 

doosri table ka header me color text ka white and cell black fill hota h. fir jb long term short term ya intraday select krte h to wo cell black fill hhi rehtee h second table ke unko white krna h/ ho jay jb bhi long term short term ya intraday select ho taki acchi dikhe . 

4. output sheet me data rewrite A4:W range me hi ho. baki jageh jo manually likh h wo mita rahe ho tum usse touch na karo esa krnado .

smjhe