Changes for page 2 Script

Last modified by Devin Chen on 2026/03/10 10:53

From version 20.1
edited by Devin Chen
on 2026/03/05 16:07
Change comment: There is no comment for this version
To version 18.1
edited by Devin Chen
on 2026/03/05 15:57
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -2048,87 +2048,8 @@
2048 2048  
2049 2049  (% style="text-align:center" %)
2050 2050  [[image:RW7OUCVVp1.png||height="617" width="943"]]
2051 +
2052 +
2051 2051  {{/info}}
2052 2052  
2053 -**3. V-box settings**
2054 -
2055 -3.1 Real-time tags setting
2056 -
2057 -(% style="text-align:center" %)
2058 -[[image:PixPin_2026-03-05_16-00-15.png||height="291" width="982"]]
2059 -
2060 -3.2 Lua Script:
2061 -
2062 -3.2.1 Basic Configuration Information
2063 -
2064 -(% style="text-align:center" %)
2065 -[[image:PixPin_2026-03-05_16-02-35.png||height="451" width="480"]]
2066 -
2067 -3.2.2 Lua Script Demo
2068 -
2069 -{{code language="Lua"}}
2070 --- Google Apps Script deployment URL (required)
2071 -local req_url = "https://script.google.com/macros/s/AKfycbxbAxKSysisJKdXeL5k1IuH4mYhnN2qOuq9ZbTtYJRQMSBgYi67eaqZRrS4JmhsA2dL/exec"
2072 -
2073 -function sheet.main()
2074 - -- Entry point: trigger the data sending process
2075 - prepareAndSendSheetData()
2076 -end
2077 -
2078 --- Prepares data from PLC addresses and sends it to Google Sheets
2079 --- Fetches: name (string) and score (word) from specified memory addresses
2080 --- Constructs JSON payload and triggers HTTP POST request
2081 -
2082 -function prepareAndSendSheetData()
2083 -
2084 - float_num = addr_getfloat("@floating number")
2085 - -- Build the data payload to be sent to Google Sheets
2086 - local SheetData = {
2087 - sheetName = "Sheet1", -- Target sheet name
2088 - stringData = addr_getstring("@string", 20), -- Fetch string from address (max 20 chars)
2089 - integerNumber = addr_getword("@word"), -- Fetch 16-bit integer from address
2090 - floatingNumber = string.format("%0.4f", float_num) -- Fetch 32-bit floating number from address
2091 - }
2092 -
2093 - -- Directly use the single dataset as the message
2094 - local message = SheetData
2095 -
2096 - -- Debug output (optional, can be removed in production)
2097 - print("URL: " .. tostring(req_url))
2098 - print("Request Body: " .. json.encode(message))
2099 -
2100 - -- Send the request via HTTP POST
2101 - executeHttpPostRequest(req_url, message)
2102 -end
2103 -
2104 -
2105 --- Executes HTTP POST request to Google Apps Script endpoint
2106 --- @param req_url: target URL for the POST request
2107 --- @param message: Lua table containing data to be sent (will be JSON-encoded)
2108 -
2109 -
2110 -function executeHttpPostRequest(req_url, message)
2111 - -- Required libraries
2112 - local json = require("json")
2113 - local ltn12 = require("ltn12")
2114 - local https = require("https")
2115 -
2116 - -- Encode the message table into a JSON string
2117 - local request_body = json.encode(message)
2118 - local max_redirects = 0 -- Number of redirects to follow (set to 0 for none)
2119 - local current_url = req_url
2120 -
2121 - local response_body = {}
2122 - local res, code, response_headers = https.request{
2123 - url = current_url,
2124 - method = "POST",
2125 - headers = {
2126 - ["Content-Type"] = "application/json", -- Must be text/plain to bypass CORS preflight
2127 - ["Content-Length"] = #request_body
2128 - },
2129 - source = ltn12.source.string(request_body),
2130 - sink = ltn12.sink.table(response_body)
2131 - }
2132 -
2133 -end
2134 -{{/code}}
2055 +
PixPin_2026-03-05_16-00-15.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -187.2 KB
Content
PixPin_2026-03-05_16-02-35.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -60.0 KB
Content