Skip to content

Instantly share code, notes, and snippets.

@medoevruslan
Last active February 5, 2024 14:44
Show Gist options
  • Save medoevruslan/9cf3c7b5f007b2575f2e27dbc079326c to your computer and use it in GitHub Desktop.
Save medoevruslan/9cf3c7b5f007b2575f2e27dbc079326c to your computer and use it in GitHub Desktop.

Revisions

  1. medoevruslan revised this gist Feb 5, 2024. 1 changed file with 3 additions and 22 deletions.
    25 changes: 3 additions & 22 deletions export-to-excel
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,7 @@
    import * as XLSX from 'xlsx'

    const handleExportExcel = () => {
    if (isLoading || users.length === 0) {
    return // Do nothing if data is still loading or no users are available
    }

    // Filter the users based on the search input values
    const filteredUsers = users.filter((user) => {
    const firstNameMatch = user.first_name
    .toLowerCase()
    .includes(searchFirstName.toLowerCase())
    const lastNameMatch = user.last_name
    .toLowerCase()
    .includes(searchLastName.toLowerCase())
    const emailMatch = user.email
    .toLowerCase()
    .includes(searchEmail.toLowerCase())
    const uniqueIdMatch = user.unique_id
    .toLowerCase()
    .includes(searchUniqueId.toLowerCase())

    return firstNameMatch && lastNameMatch && emailMatch && uniqueIdMatch
    })


    const filename = 'members.xlsx'
    const wsData = [
    ['First Name', 'Last Name', 'Email', 'Unique ID', 'Plan Name', 'Status'],
  2. medoevruslan created this gist Feb 5, 2024.
    42 changes: 42 additions & 0 deletions export-to-excel
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    const handleExportExcel = () => {
    if (isLoading || users.length === 0) {
    return // Do nothing if data is still loading or no users are available
    }

    // Filter the users based on the search input values
    const filteredUsers = users.filter((user) => {
    const firstNameMatch = user.first_name
    .toLowerCase()
    .includes(searchFirstName.toLowerCase())
    const lastNameMatch = user.last_name
    .toLowerCase()
    .includes(searchLastName.toLowerCase())
    const emailMatch = user.email
    .toLowerCase()
    .includes(searchEmail.toLowerCase())
    const uniqueIdMatch = user.unique_id
    .toLowerCase()
    .includes(searchUniqueId.toLowerCase())

    return firstNameMatch && lastNameMatch && emailMatch && uniqueIdMatch
    })

    const filename = 'members.xlsx'
    const wsData = [
    ['First Name', 'Last Name', 'Email', 'Unique ID', 'Plan Name', 'Status'],
    // Loop through the filtered users array and extract the required data
    ...filteredUsers.map((user) => [
    user.first_name,
    user.last_name,
    user.email,
    user.unique_id,
    user.account_type,
    user.activated === false ? 'Inactive' : 'Active',
    ]),
    ]

    const ws = XLSX.utils.aoa_to_sheet(wsData)
    const wb = XLSX.utils.book_new()
    XLSX.utils.book_append_sheet(wb, ws, 'Members')
    XLSX.writeFile(wb, filename)
    }