// ... [Your existing code remains unchanged up to the end] ...

// New functions for auto-capturing client information

/**
 * 1. Auto-save functionality
 * This function should be called from your front-end JavaScript
 * @param {Object} partialData - The partial form data to be saved
 */
function autoSave(partialData) {
  const formSheet = getFormSheet(getSheetName(partialData));
  const headers = getHeaders(formSheet, Object.keys(partialData));
  const values = getValues(headers, partialData);
  
  // Check if there's an existing row for this user (you might want to use a unique identifier)
  const lastRow = formSheet.getLastRow();
  const existingRowIndex = findExistingRow(formSheet, partialData['unique_id'], lastRow);
  
  if (existingRowIndex) {
    // Update existing row
    formSheet.getRange(existingRowIndex, 1, 1, values.length).setValues([values]);
  } else {
    // Add new row
    setValues(formSheet, values);
  }
}

/**
 * Helper function to find an existing row based on a unique identifier
 * @param {Sheet} sheet - The Google Sheet
 * @param {string} uniqueId - A unique identifier for the user
 * @param {number} lastRow - The last row of the sheet
 * @returns {number|null} - The row index if found, null otherwise
 */
function findExistingRow(sheet, uniqueId, lastRow) {
  if (lastRow <= 1) return null; // Only header row exists
  const idColumn = sheet.getRange(2, 1, lastRow - 1, 1).getValues();
  for (let i = 0; i < idColumn.length; i++) {
    if (idColumn[i][0] === uniqueId) return i + 2; // +2 because array is 0-indexed and we start from row 2
  }
  return null;
}

/**
 * 2. Exit-intent capture
 * This function should be called when exit intent is detected on the front-end
 * @param {Object} formData - The complete form data to be submitted
 */
function exitIntentCapture(formData) {
  insertToSheet(formData);
}

/**
 * 3. Timer-based submission
 * This function should be called after a period of inactivity on the front-end
 * @param {Object} formData - The form data collected so far
 */
function timerBasedSubmission(formData) {
  insertToSheet(formData);
}

/**
 * 4. Progressive form submission
 * This function should be called each time a section of the form is completed
 * @param {Object} sectionData - The data from the completed section
 * @param {string} sectionName - The name of the completed section
 */
function progressiveSubmission(sectionData, sectionName) {
  const formSheet = getFormSheet(getSheetName(sectionData));
  const headers = getHeaders(formSheet, [...Object.keys(sectionData), 'section_name']);
  const values = [...getValues(headers, sectionData), sectionName];
  
  setValues(formSheet, values);
}

// You may need to add this new property to your existing code
const UNIQUE_ID_PROPERTY = 'unique_id';

// Modify your existing insertToSheet function to handle partial submissions
const insertToSheet = (data) => {
  const flat = flattenObject(data);
  const keys = Object.keys(flat);
  const formSheet = getFormSheet(getSheetName(data));
  const headers = getHeaders(formSheet, keys);
  const values = getValues(headers, flat);

  // Check if this is an update to an existing row
  if (data[UNIQUE_ID_PROPERTY]) {
    const lastRow = formSheet.getLastRow();
    const existingRowIndex = findExistingRow(formSheet, data[UNIQUE_ID_PROPERTY], lastRow);
    if (existingRowIndex) {
      // Update existing row
      formSheet.getRange(existingRowIndex, 1, 1, values.length).setValues([values]);
      return;
    }
  }

  // If not an update, proceed with inserting a new row
  setHeaders(formSheet, headers);
  setValues(formSheet, values);

  if (emailNotification) {
    sendNotification(data, getSheetURL());
  }
}                  this is how i capture  client  information, but there is a problem most of my client forget to presse the button "buy now " that way i can't get there information , is there a way this code that i give you and let it capture clients info without pressing any button in my form