|


Add References ... ใช้ Nuget Package Manager จะสะดวกกว่า

ข้อมูลตัวอย่างใน Google Sheet ...
โค้ดตรวจสอบวันที่หมดอายุการใช้งานของโปรแกรมด้วยการใช้ Google Sheet เป็นที่เก็บข้อมูล ผ่านทาง Google Visualization API ...
คลิปวิดีโอประกอบคำอธิบายบางส่วน ...
มาดูโค้ดฉบับเต็มกันเถอะ ...
- Imports System.Net
- '// Download MaterialSkin.2 packages.
- '// [url]https://www.nuget.org/packages/MaterialSkin.2/[/url]
- Imports MaterialSkin
- Imports Newtonsoft.Json.Linq
- '// คลิปวิดีโอการรับไฟล์ Credentials เพื่อทำการติดต่อกับ Google Sheets และการกำหนดสิทธิ์การเข้าถึง
- '// [url]https://www.youtube.com/watch?v=xdYsctNAGEE[/url]
- '// ------------------------------------------------------------------------------------------------
- '// Google Visualization API Reference ... [url]https://developers.google.com/chart/interactive/docs/reference[/url]
- '// Google Visualization API
- '// เป็น API จาก Google ที่ช่วยให้เราสามารถดึงข้อมูลจากแหล่งข้อมูลต่างๆ เช่น Google Sheets หรือไฟล์ข้อมูลอื่นๆ
- '// แล้วนำข้อมูลนั้นมา แสดงผลในรูปแบบกราฟ, ตาราง หรือ Visualization ต่างๆบนเว็บได้ง่ายๆ
- '// เหมาะสำหรับการสร้างแดชบอร์ด หรือรายงานแบบ Interactive ที่ทำงานบนเว็บ
- '// Google Visualization API มีฟอร์แมตข้อมูลเฉพาะที่เรียกว่า DataTable (เหมือนตารางฐานข้อมูล มีคอลัมน์และแถว)
- '// เราสามารถดึงข้อมูลผ่าน URL โดยใช้ Google Visualization Query Language (GVQL) ซึ่งคล้าย SQL
- '// ------------------------------------------------------------------------------------------------
- Public Class frmLogin
- '// ป้อน Spreadsheet ID และชื่อชีต
- Private Const SpreadsheetId As String = "SPREAD_SHEET_ID" '// <-- เปลี่ยนเป็นของคุณ
- 'Private Const ApplicationName As String = "" '// ไม่ได้ใช้งาน
- Private Const SheetName As String = "SHEET_NAME"
- Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- '// ข้อมูลตัวอย่าง
- txtUserName.Text = "admin"
- txtPassword.Text = "admin"
- With Me
- .MinimumSize = New Point(451, 503)
- .MaximumSize = New Point(451, 503)
- End With
- '// Code sample.
- '// [url]https://www.nuget.org/packages/MaterialSkin.2/[/url]
- Dim SkinManager As MaterialSkinManager = MaterialSkinManager.Instance
- SkinManager.AddFormToManage(Me)
- SkinManager.Theme = MaterialSkinManager.Themes.DARK
- SkinManager.ColorScheme = New ColorScheme(Primary.Amber500, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE)
- '// ตัวอย่างของ ColorScheme
- 'SkinManager.ColorScheme = New ColorScheme(Primary.Blue300, Primary.Blue500, Primary.Blue500, Accent.Blue400, TextShade.WHITE)
- 'SkinManager.ColorScheme = New ColorScheme(Primary.Green600, Primary.Green700, Primary.Green200, Accent.Red100, TextShade.WHITE)
- 'SkinManager.ColorScheme = New ColorScheme(Primary.LightBlue600, Primary.LightBlue700, Primary.Green200, Accent.LightGreen700, TextShade.WHITE)
- 'SkinManager.ColorScheme = New ColorScheme(Primary.Cyan500, Primary.Cyan700, Primary.Cyan100, Accent.Blue100, TextShade.WHITE)
- End Sub
- Private Sub swPassword_CheckedChanged(sender As Object, e As EventArgs) Handles swPassword.CheckedChanged
- txtPassword.Focus()
- txtPassword.Password = Not swPassword.Checked
- End Sub
- ' / ------------------------------------------------------------------------------------------------
- ' / เริ่มต้นการเข้าสู่ระบบ
- ' / ------------------------------------------------------------------------------------------------
- Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
- Dim UserName As String = txtUserName.Text.Trim()
- Dim Password As String = txtPassword.Text.Trim()
- If String.IsNullOrEmpty(UserName) OrElse String.IsNullOrEmpty(Password) Then
- MessageBox.Show("กรุณากรอก Username และ Password", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- Return
- End If
- '//
- '// ใช้ Google Visualization API เพื่อดึงข้อมูลทั้งหมด
- Dim SheetUrl As String = "https://docs.google.com/spreadsheets/d/" & SpreadsheetId & "/gviz/tq?tqx=out:json&sheet=" & SheetName
- Try
- Using Client As New WebClient()
- Dim JsonRaw As String = Client.DownloadString(SheetUrl)
- '// ตัดส่วนหัวออก (Google ใส่ "/*O_o*/" และ "google.visualization.Query.setResponse(...)")
- Dim startIndex = JsonRaw.IndexOf("(") + 1
- Dim endIndex = JsonRaw.LastIndexOf(")")
- Dim jsonData = JsonRaw.Substring(startIndex, endIndex - startIndex)
- '//
- Dim jObj As JObject = JObject.Parse(jsonData)
- Dim rows As JArray = jObj("table")("rows")
- '// ตัวอย่าง ... Columns ประกอบด้วย {UserPK, UserName, Password, ExpireDate}
- '// {
- '// "table" {
- '// "rows": [
- '// { "c": [ {"v": "1"}, {"v": "admin"}, {"v": "admin"}, {"v": "30/05/2568"} ] },
- '// { "c": [ {"v": "2"}, {"v": "user"}, {"v": "user"}, {"v": "21/05/2568"} ] }
- '// ]
- '// }
- '// }
- For Each row As JObject In rows
- Dim cols As JArray = row("c")
- Dim userPK = cols(0)("v").ToString()
- Dim user = cols(1)("v").ToString()
- Dim pass = cols(2)("v").ToString()
- Dim expireDate = cols(3)("v").ToString()
- '// จัดรูปแบบวันที่ก่อน
- Dim ExpireDateTime As DateTime? = ParseDateFunctionString(expireDate)
- If user.Equals(UserName, StringComparison.OrdinalIgnoreCase) AndAlso pass.Equals(Password) Then
- If ExpireDateTime >= DateTime.Now Then
- MessageBox.Show("เข้าสู่ระบบสำเร็จ!", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
- frmMain.Show()
- Me.Hide()
- Return
- Else
- MessageBox.Show("บัญชีหมดอายุ กรุณาติดต่อผู้ดูแลระบบ", "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
- Application.Exit()
- Return
- End If
- End If
- Next
- MessageBox.Show("Username หรือ Password ไม่ถูกต้อง!!!", "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- End Using
- Catch ex As Exception
- MessageBox.Show("เกิดข้อผิดพลาด: " & ex.Message, "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End Sub
- ' / ------------------------------------------------------------------------------------------------
- ' / ต้องเขียนโค้ดดึงค่าปี เดือน วัน ออกจาก string "Date(2568,5,30)" ก่อน
- ' / Google Sheet นับเดือน 1 เป็น 0 เราจึงต้อง +1 เข้าไป
- ' / ------------------------------------------------------------------------------------------------
- Function ParseDateFunctionString(rawDate As String) As DateTime?
- '// ตรวจสอบรูปแบบ "Date(2568,5,30)"
- Dim pattern As String = "^Date\((\d+),(\d+),(\d+)\)[ DISCUZ_CODE_0 ]quot;
- Dim m As Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(rawDate, pattern)
- If m.Success Then
- Dim yearBE As Integer = Integer.Parse(m.Groups(1).Value)
- Dim month As Integer = Integer.Parse(m.Groups(2).Value) + 1 '// เพราะ Google เริ่มนับเดือนที่ 0 เลยต้อง +1 เข้าไป
- Dim day As Integer = Integer.Parse(m.Groups(3).Value)
- '// แปลงปี พ.ศ. เป็น ค.ศ.
- Dim yearAD As Integer = yearBE
- If yearBE > 2500 Then yearAD = yearBE - 543
- Try
- Dim MyDateTime As New DateTime(yearAD, month, day)
- Return MyDateTime
- Catch ex As Exception
- Return Nothing
- End Try
- Else
- Return Nothing
- End If
- End Function
- Private Sub txtUserName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtUserName.KeyPress
- '// Press ENTER
- If Asc(e.KeyChar) = 13 Then
- e.Handled = True '// No beep
- SendKeys.Send("{TAB}")
- End If
- End Sub
- Private Sub txtPassword_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPassword.KeyPress
- '// Press ENTER
- If Asc(e.KeyChar) = 13 Then
- e.Handled = True '// No beep
- SendKeys.Send("{TAB}")
- End If
- End Sub
- Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
- Me.Close()
- End Sub
- Private Sub frmLogin_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
- Me.Dispose()
- GC.SuppressFinalize(Me)
- Application.Exit()
- End Sub
- End Class
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับ VB.NET & .Net Framework 4.6.2+ ได้ที่นี่ ...
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|