Fetching infromation from Google Calendar API to turn my calendar into a personal income calculator

As a gig artist, I write all my gigs in Google Calendar, and I’m tired of having to read through 40 gigs/month to calculate my monthly income.

I want to create a webpage that will access my calendar, find events that contain “+(income number here)” then calculate and display my monthly income.

While I’m doing this, I figure I might as well make it read “-(expense number here)” events and have it calculate my expenses as well.

I’m not a great programmer tho, only proficient in HTML and CSS, and pretty bad at Javascript, so this is what I got from Chat GPT

(Please note I inserted the API Key and CLIENT ID although I don’t know if I set everything up right on google’s side.)

This first draft isn’t working at all. Can anyone point me in the right direction?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Google Calendar Events</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 800px;
            margin: 20px auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
        }

        .numbers {
            display: flex;
            justify-content: space-around;
            margin-top: 20px;
        }

        .numbers div {
            text-align: center;
            padding: 10px;
            border-radius: 8px;
            background-color: #f5f5f5;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        h2 {
            margin-bottom: 10px;
        }

        p {
            font-size: 24px;
            font-weight: bold;
        }

        footer {
            text-align: center;
            margin-top: 20px;
            padding: 10px;
            background-color: #ddd;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Event Statistics</h1>
        <div class="numbers">
            <div>
                <h2>Total Increase</h2>
                <p id="totalIncrease">Loading...</p>
            </div>
            <div>
                <h2>Total Decrease</h2>
                <p id="totalDecrease">Loading...</p>
            </div>
        </div>
    </div>

    <footer id="footer">
        <div id="userStatus">
            <!-- Placeholder for login status -->
        </div>
        <div id="authBtnContainer">
            <!-- Placeholder for login button -->
        </div>
    </footer>

    <script>
        // Replace with your Client ID and API key
        const CLIENT_ID = 'YOUR_CLIENT_ID';
        const API_KEY = 'YOUR_API_KEY';

        // Function to load Google Calendar API
        function loadClient() {
            gapi.client.setApiKey(API_KEY);
            return gapi.client.load('calendar', 'v3');
        }

        // Function to check user authentication status
        function checkAuthStatus() {
            gapi.auth2.getAuthInstance().isSignedIn.listen(updateUserStatus);
            updateUserStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
        }

        // Update user status in the footer
        function updateUserStatus(isSignedIn) {
            const userStatusContainer = document.getElementById('userStatus');
            const authBtnContainer = document.getElementById('authBtnContainer');

            if (isSignedIn) {
                const user = gapi.auth2.getAuthInstance().currentUser.get();
                const userName = user.getBasicProfile().getName();
                userStatusContainer.textContent = `Logged in as: ${userName}`;
                authBtnContainer.innerHTML = '<button onclick="signOut()">Sign Out</button>';
            } else {
                userStatusContainer.textContent = 'Not logged in';
                authBtnContainer.innerHTML = '<button onclick="signIn()">Sign In</button>';
            }
        }

        // Function to handle sign in
        function signIn() {
            gapi.auth2.getAuthInstance().signIn().then(() => {
                console.log('User signed in.');
                loadClient().then(calculateEventTotals).catch(err => console.error('Error loading client:', err));
            }).catch(err => console.error('Sign in error:', err));
        }

        // Function to handle sign out
        function signOut() {
            gapi.auth2.getAuthInstance().signOut().then(() => {
                console.log('User signed out.');
                updateUserStatus(false);
            }).catch(err => console.error('Sign out error:', err));
        }

        // Function to calculate increase and decrease totals
        async function calculateEventTotals() {
            await loadClient();

            // Calculate total increase
            const increaseQuery = await gapi.client.calendar.events.list({
                'calendarId': 'primary',
                'timeMin': new Date().toISOString(),
                'q': '+'
            });

            const totalIncrease = increaseQuery.result.items.reduce((acc, event) => {
                const match = event.summary.match(/+(d+)/);
                if (match) {
                    return acc + parseInt(match[1]);
                }
                return acc;
            }, 0);

            // Calculate total decrease
            const decreaseQuery = await gapi.client.calendar.events.list({
                'calendarId': 'primary',
                'timeMin': new Date().toISOString(),
                'q': '-'
            });

            const totalDecrease = decreaseQuery.result.items.reduce((acc, event) => {
                const match = event.summary.match(/-(d+)/);
                if (match) {
                    return acc + parseInt(match[1]);
                }
                return acc;
            }, 0);

            // Update HTML with calculated totals
            document.getElementById('totalIncrease').textContent = totalIncrease;
            document.getElementById('totalDecrease').textContent = totalDecrease;
        }

        // Initialize Google API client and check auth status on window load
        window.onload = function () {
            gapi.load('client:auth2', function () {
                gapi.auth2.init({
                    client_id: CLIENT_ID
                }).then(() => {
                    console.log('Google API initialized.');
                    checkAuthStatus();
                    calculateEventTotals().catch(err => console.error('Error calculating totals:', err));
                }).catch(err => console.error('Error initializing Google API:', err));
            });
        };
    </script>
    <script async defer src="https://apis.google.com/js/api.js"
            onload="this.onload=function(){};handleClientLoad()"
            onreadystatechange="if (this.readyState === 'complete') this.onload()">
    </script>
</body>
</html>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật