You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

浏览器JavaScript向Delphi 7开票软件传递数据可行性问询

Can Browser-Side JavaScript in Google Sheets Send Data to a Delphi 7 Desktop App?

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() or location.href to trigger the custom URL. Example:
    const selectedRow = SpreadsheetApp.getActiveRange().getValues()[0];
    const invoiceData = `invoiceNo=${selectedRow[0]}&amount=${selectedRow[1]}&client=${encodeURIComponent(selectedRow[2])}`;
    window.open(`myinvoicer://?${invoiceData}`);
    
    Note: Browsers might show a security prompt the first time you use this—users will need to allow the link to open your app.

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 TIdHTTPServer to 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 standard fetch is restricted in Sheets’ JS environment) to send the data. Example:
    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
    });
    
    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.

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:
    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);
    
    This is the easiest to implement but requires some user interaction, so it’s less automated than the other options.

Key Things to Keep in Mind

  • Browser Security: Custom URL schemes may trigger warnings, and UrlFetchApp in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:01:08