浏览器JavaScript向Delphi 7开票软件传递数据可行性问询
Great question! The short answer is yes, but browser-based JavaScript can’t directly talk to desktop applications like your invoicing software out of the box. You’ll need to add a small bridge between the browser and your Delphi app. Here are the most practical approaches to make this work:
1. Custom URL Scheme (Simplest for Basic Data)
You can register a custom URL protocol (like myinvoicer://) with your operating system, which will route requests directly to your Delphi app. Here’s how to set it up:
- In Delphi 7: Use Windows API functions (or a lightweight third-party component) to register your custom scheme. When the browser opens a URL like
myinvoicer://?invoiceNo=INV-123&amount=499.99, your app will launch (or activate if already running) and receive the query parameters as command-line arguments. You’ll just need to parse these arguments to pull out the invoice data. - In Google Sheets JS: Use
window.open()orlocation.hrefto trigger the custom URL. Example:
Note: Browsers might show a security prompt the first time you use this—users will need to allow the link to open your app.const selectedRow = SpreadsheetApp.getActiveRange().getValues()[0]; const invoiceData = `invoiceNo=${selectedRow[0]}&amount=${selectedRow[1]}&client=${encodeURIComponent(selectedRow[2])}`; window.open(`myinvoicer://?${invoiceData}`);
2. Local HTTP Server (Best for Complex Data)
Set up a lightweight HTTP server inside your Delphi 7 app (using components like Indy, which comes pre-installed with Delphi 7) that listens on a specific port (e.g., http://localhost:8080). Then your Google Sheets JS can send an HTTP POST request to this endpoint with structured data (like JSON):
- In Delphi 7: Configure an Indy
TIdHTTPServerto handle incoming requests. When a request hits/submit-invoice, parse the JSON body and populate your invoicing form fields automatically. - In Google Sheets JS: Use
UrlFetchApp(since standardfetchis restricted in Sheets’ JS environment) to send the data. Example:
This method lets you send larger, more complex datasets and avoids browser security prompts (as long as the server is running locally). Just make sure the port isn’t blocked by your firewall.const selectedRow = SpreadsheetApp.getActiveRange().getValues()[0]; const payload = JSON.stringify({ invoiceNumber: selectedRow[0], amount: selectedRow[1], clientName: selectedRow[2] }); UrlFetchApp.fetch('http://localhost:8080/submit-invoice', { method: 'POST', contentType: 'application/json', payload: payload });
3. Clipboard Middleman (Quickest Low-Effort Hack)
For a fast, no-fuss solution, have your JS format the selected row data into a structured string (e.g., CSV or a delimiter-separated format) and copy it to the clipboard. Then your Delphi app can either:
- Listen for clipboard changes (using Windows API or Delphi components) and auto-populate when it detects the formatted data, or
- Let users manually paste the data into the app with a keyboard shortcut.
- JS Example:
This is the easiest to implement but requires some user interaction, so it’s less automated than the other options.const selectedRow = SpreadsheetApp.getActiveRange().getValues()[0]; const clipboardText = selectedRow.join('|'); // Use a unique delimiter that won't clash with your data navigator.clipboard.writeText(clipboardText);
Key Things to Keep in Mind
- Browser Security: Custom URL schemes may trigger warnings, and
UrlFetchAppin Google Sheets only works with accessible local servers (HTTPS isn’t required for localhost). - Delphi 7 Compatibility: Indy components in Delphi 7 are a bit dated, so you might need to use a third-party Delphi JSON library (like SuperObject) to parse incoming JSON data.
- Single Instance Handling: If your app is already running, make sure the custom URL or HTTP request triggers the existing instance instead of launching a new one (use Windows mutexes or IPC in Delphi to manage this).
内容的提问来源于stack exchange,提问作者Steve

