Untitled
user_1344937
plain_text
22 days ago
4.9 kB
2
Indexable
Never
Imports Google.Apis.Sheets.v4 Imports Google.Apis.Sheets.v4.Data Imports System.Data Imports System.Globalization Public Class FormMenu1 Private service As SheetsService Private spreadsheetId As String Private sheetName As String Private dataTable As DataTable Public Sub New(googleSheetsService As SheetsService, sheetId As String, sheet As String) InitializeComponent() service = googleSheetsService spreadsheetId = sheetId sheetName = sheet dataTable = New DataTable() dataTable.Columns.AddRange(New DataColumn() { New DataColumn("รหัสพนักงาน"), New DataColumn("ชื่อ-สกุล"), New DataColumn("เลขบัตรประชาชน"), New DataColumn("วันเกิด"), New DataColumn("เบอร์ติดต่อ"), New DataColumn("วันเริ่มงาน"), New DataColumn("แผนก"), New DataColumn("สถานะพนักงาน"), New DataColumn("ที่อยู่") }) DataGridView1.DataSource = dataTable End Sub Private Sub FormMenu1_Load(sender As Object, e As EventArgs) Handles MyBase.Load DateTimePickerBirthDate.Format = DateTimePickerFormat.Custom DateTimePickerBirthDate.CustomFormat = "dd/MM/yyyy" DateTimePickerStartDate.Format = DateTimePickerFormat.Custom DateTimePickerStartDate.CustomFormat = "dd/MM/yyyy" End Sub Private Sub DateTimePickerBirthDate_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePickerBirthDate.ValueChanged DateTimePickerBirthDate.CustomFormat = "dd/MM/yyyy" End Sub Private Sub DateTimePickerStartDate_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePickerStartDate.ValueChanged DateTimePickerStartDate.CustomFormat = "dd/MM/yyyy" End Sub Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click Dim row As DataRow = dataTable.NewRow() row("รหัสพนักงาน") = txtEmployeeID.Text row("ชื่อ-สกุล") = txtFullName.Text row("เลขบัตรประชาชน") = txtIDCardNumber.Text row("วันเกิด") = DateTimePickerBirthDate.Value.ToString("yyyy-MM-dd") row("เบอร์ติดต่อ") = txtContactNumber.Text row("วันเริ่มงาน") = DateTimePickerStartDate.Value.ToString("yyyy-MM-dd") row("แผนก") = txtDepartment.Text row("สถานะพนักงาน") = txtEmployeeStatus.Text row("ที่อยู่") = txtAddress.Text dataTable.Rows.Add(row) txtEmployeeID.Clear() txtFullName.Clear() txtIDCardNumber.Clear() DateTimePickerBirthDate.Value = DateTime.Today DateTimePickerStartDate.Value = DateTime.Today txtContactNumber.Clear() txtDepartment.SelectedIndex = -1 txtEmployeeStatus.SelectedIndex = -1 txtAddress.Clear() End Sub Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click Try Dim range As String = $"{sheetName}!A:I" Dim valueRange As New ValueRange() Dim objectList As New List(Of IList(Of Object)) For Each row As DataRow In dataTable.Rows objectList.Add(New List(Of Object) From { row("รหัสพนักงาน"), row("ชื่อ-สกุล"), row("เลขบัตรประชาชน"), row("วันเกิด"), row("เบอร์ติดต่อ"), row("วันเริ่มงาน"), row("แผนก"), row("สถานะพนักงาน"), row("ที่อยู่") }) Next valueRange.Values = objectList Dim appendRequest = service.Spreadsheets.Values.Append(valueRange, spreadsheetId, range) appendRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED appendRequest.Execute() MessageBox.Show("บันทึกข้อมูลเรียบร้อย", "การบันทึก", MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show("เกิดข้อผิดพลาด: " & ex.Message, "ข้อผิดพลาดในการบันทึก", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub End Class
Leave a Comment