Saturday, August 16, 2025

🟢3.Modified News Agency Invoise blog post.Added3

Create a Subscription and Invoice Management System for Newspapers in Google Sheets

Create a Subscription and Invoice Management System for Newspapers in Google Sheets

Hello, Google Sheets enthusiasts! Are you running a small newspaper agency and looking to automate subscriber management, invoice generation, and payment tracking? This Google Apps Script solution creates a powerful subscription and invoice management system right in your Google Sheet. It includes features like adding subscribers, generating invoices with a dropdown interface, tracking payments, exporting PDFs, creating summary reports, and sending invoices via WhatsApp. Below, you’ll find the main script, an HTML form, a WhatsApp integration script, and an invoice generation script, each with copy buttons for easy setup.

What This System Does

This system offers a robust set of tools for managing newspaper subscriptions:

  • A custom HTML form to add or update subscriber details.
  • A Subscribers sheet to store customer data (contact number, name, address, subscription type, last payment date).
  • An Invoices sheet to log all invoices with details like Invoice ID, contact number, items, amount, date, status, and PDF URL.
  • An Items sheet to store predefined items and their prices (used in invoice generation).
  • A Payments sheet to record payment details (contact, subscription type, date, amount, notes).
  • A dropdown-based invoice creation feature to select items and quantities.
  • An export to PDF feature that saves invoices to Google Drive and emails them.
  • A batch PDF generation feature for all pending invoices.
  • A summary report feature to calculate total invoiced and paid amounts.
  • A backup feature to save the spreadsheet to Google Drive.
  • A WhatsApp integration to send formatted invoice messages with a clickable link.
  • A custom menu in Google Sheets to access all features.

Step-by-Step Setup Instructions

  1. Create a New Google Sheet:
    • Open Google Sheets and create a new spreadsheet (e.g., "Newspaper Subscription System").
    • Save it to your Google Drive.
  2. Open the Apps Script Editor:
    • In your Google Sheet, go to Extensions > Apps Script.
  3. Add the Main Google Apps Script Code:
    • In the Apps Script editor, clear any code in Code.gs.
    • Copy the code from Code Block 1 and paste it into Code.gs.
    • Save the file (Ctrl + S) and name the project (e.g., "Subscription System").
  4. Add the HTML Form:
    • In the Apps Script editor, go to File > New > HTML file.
    • Name it SubscriberForm (exact name required).
    • Copy the code from Code Block 2 and paste it into SubscriberForm.html.
    • Save the file.
  5. Add the WhatsApp Integration Code:
    • In the Apps Script editor, go to File > New > Script file.
    • Name it WhatsApp.gs (exact name recommended).
    • Copy the code from Code Block 3 and paste it into WhatsApp.gs.
    • Save the file.
  6. Add the Invoice Generation Code:
    • In the Apps Script editor, go to File > New > Script file.
    • Name it InvoiceGeneration.gs (exact name recommended).
    • Copy the code from Code Block 4 and paste it into InvoiceGeneration.gs.
    • Save the file.
    • Note: The invoice generation code relies on functions in Code.gs (initializeSheets, validateContact, getSubscriberByContact, generateInvoicePDFDirect, and populateInvoiceSheet). Ensure Code.gs is set up first.
  7. Set Up the Items Sheet:
    • In your Google Sheet, create an Items sheet or let the script create it automatically.
    • Add item names in column C (C4:C6) and their prices in column D (D4:D6). For example:
      • C4: Daily Newspaper, D4: 5.00
      • C5: Weekly Magazine, D5: 20.00
      • C6: Monthly Subscription, D6: 100.00
  8. Configure the Backup Folder:
    • In the backupSheet function in Code.gs, replace YOUR_FOLDER_ID with the ID of a Google Drive folder where backups will be stored. To find the folder ID, open the folder in Google Drive, and copy the ID from the URL (e.g., https://drive.google.com/drive/folders/[FOLDER_ID]).
  9. Set Up Permissions:
    • Click Run in the Apps Script editor to execute the onOpen function.
    • Authorize the script by reviewing and granting permissions (Sheets, Drive, Gmail, Document).
    • A custom menu (Subscription Management) will appear in your Google Sheet.
  10. Set Up Sample Data (Optional):
    • Manually add sample subscribers to the Subscribers sheet (e.g., contact number, name, address, subscription type).
    • Add sample items to the Items sheet as described above.

How to Use the Subscription System

  1. Add or Update Subscribers:
    • Via Form: Go to Subscription Management > Add/Update Subscriber (Form) to open the HTML form. Enter a 10-digit contact number, name, address, and subscription type. If the contact exists, the form auto-fills the name and address.
    • Via Prompt: Select Add/Update Subscriber (Prompt) and enter details through dialog boxes.
  2. Create an Invoice:
    • Go to Subscription Management > Create Invoice (Dropdown).
    • Enter a 10-digit contact number. If the subscriber exists, their name auto-fills; otherwise, enter a name.
    • Select items and quantities from the dropdown (populated from the Items sheet, C4:C6).
    • Choose an invoice date (defaults to today).
    • The invoice is logged in the Invoices sheet, a PDF is generated, and it’s emailed to your Google account.
    • An Invoice sheet is created/updated with the invoice details for WhatsApp integration.
  3. Send Invoice via WhatsApp:
    • After creating an invoice, go to the Invoice sheet.
    • Run the WhatsAppWordsWithVariedTotalRowFixed function from the Apps Script editor or add it to the custom menu (see Tips and Troubleshooting).
    • A WhatsApp hyperlink is generated in cell D1 of the Invoice sheet. Click it to open WhatsApp with a pre-formatted invoice message.
  4. Check Subscriptions:
    • Select Check Subscriptions, enter a contact number, and view the subscriber’s details (name, subscription types, last payment dates).
  5. Record a Payment:
    • Go to Record Payment, enter the contact number, subscription type, amount, date, and optional notes.
    • The payment is logged in the Payments sheet, and the Subscribers sheet updates the last payment date.
  6. Export Invoice PDF:
    • Select Export Invoice PDF, enter an Invoice ID (e.g., INV001), and regenerate the PDF for that invoice.
    • The PDF is saved to the AppleInvoiceFolder in Google Drive and emailed to your account.
  7. Generate All Pending Invoice PDFs:
    • Choose Generate All Pending Invoice PDFs to create PDFs for all invoices marked as "Pending" in the Invoices sheet.
    • PDFs are saved to Google Drive and emailed.
  8. Create a Summary Report:
    • Select Create Summary Report to generate a Summary sheet with total invoiced and paid amounts.
  9. Backup the Sheet:
    • Choose Backup Sheet to save a copy of the spreadsheet to the specified Google Drive folder.

Code Blocks to Copy

Below are the four code blocks. Click the Copy button for each to copy the code to your clipboard, then paste it into the appropriate file in your Apps Script editor.

Code Block 1: Code.gs (Main Script)
Code Block 2: SubscriberForm.html (HTML Form)
Code Block 3: WhatsApp.gs (WhatsApp Integration)
Code Block 4: InvoiceGeneration.gs (Invoice Generation)

Tips and Troubleshooting

  • Validation: The system ensures 10-digit contact numbers and requires names for subscribers and invoices.
  • Items Sheet: Ensure valid items and prices are listed in C4:C6 and D4:D6 of the Items sheet for invoice creation.
  • PDF Export: PDFs are saved in a Google Doc format with a table of items, stored in the AppleInvoiceFolder, and emailed to your Google account.
  • WhatsApp Integration: Ensure the Invoice sheet is populated (by creating an invoice) before running the WhatsAppWordsWithVariedTotalRowFixed function. The hyperlink in cell D1 requires a valid 10-digit phone number.
  • Backup: Replace YOUR_FOLDER_ID in the backupSheet function with a valid Google Drive folder ID to enable backups.
  • Add WhatsApp to Custom Menu: To add the WhatsApp feature to the custom menu, modify the onOpen function in Code.gs to:
    function onOpen() {
      const ui = SpreadsheetApp.getUi();
      ui.createMenu('Subscription Management')
        .addItem('Add/Update Subscriber (Form)', 'showSubscriberForm')
        .addItem('Add/Update Subscriber (Prompt)', 'addOrUpdateSubscriber')
        .addItem('Create Invoice (Dropdown)', 'generateInvoicetest')
        .addItem('Check Subscriptions', 'checkSubscriptions')
        .addItem('Record Payment', 'recordPayment')
        .addItem('Export Invoice PDF', 'exportInvoicePDF')
        .addItem('Generate All Pending Invoice PDFs', 'generateAllPendingInvoicePDFs')
        .addItem('Create Summary Report', 'createSummaryReport')
        .addItem('Backup Sheet', 'backupSheet')
        .addItem('Send WhatsApp Invoice', 'WhatsAppWordsWithVariedTotalRowFixed')
        .addToUi();
    }
          
  • Errors:
    • "Invalid contact number": Ensure the contact number is 10 digits.
    • "Subscriber not found": Verify the contact number exists in the Subscribers sheet.
    • "No valid items found": Check that the Items sheet has valid entries in C4:C6 and D4:D6.
    • "PDF not emailed": Confirm Gmail permissions and your account’s email address.
    • "Invalid date": Ensure dates are in YYYY-MM-DD format.
    • "Phone number is missing or invalid" (WhatsApp): Verify the phone number in the Invoice sheet is valid.
    • "No items found" (WhatsApp): Ensure the Invoice sheet has valid items listed starting from row 7.
    • "Script function not found": Ensure all script files (Code.gs, WhatsApp.gs, InvoiceGeneration.gs) are correctly named and saved.
  • Customize: Modify the HTML form’s CSS for styling, adjust the PDF format in generateInvoicePDFDirect, tweak the WhatsApp message format in WhatsAppWordsWithVariedTotalRowFixed, or enhance the summary report in createSummaryReport.

Note: The invoice generation functions in InvoiceGeneration.gs depend on initializeSheets, validateContact, getSubscriberByContact, generateInvoicePDFDirect, and populateInvoiceSheet from Code.gs. Ensure Code.gs is set up before running invoice-related functions. To make the WhatsApp feature more accessible, add it to the custom menu as shown above.

No comments:

Post a Comment