From e914415d5684ee8b440a9109f85d75f39bc653f6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mikl=C3=B3s=20T=C3=B3th?= <tothmiklostibor@gmail.com>
Date: Fri, 17 Jun 2022 15:04:26 +0200
Subject: [PATCH] Fix off by one

---
 src/jsMain/kotlin/datasources/GSheet.kt |  8 ++++----
 src/jsMain/kotlin/ui/SheetsModal.kt     | 16 +++++++++++++++-
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/jsMain/kotlin/datasources/GSheet.kt b/src/jsMain/kotlin/datasources/GSheet.kt
index 08c689e..387b763 100644
--- a/src/jsMain/kotlin/datasources/GSheet.kt
+++ b/src/jsMain/kotlin/datasources/GSheet.kt
@@ -9,17 +9,17 @@ import pekstuff.trimAsPrinciple
 import kotlin.coroutines.resume
 import kotlin.coroutines.suspendCoroutine
 
-class GSheet private constructor(sheet: sheets.Spreadsheet): DataSource, NoteDataSource {
+class GSheet private constructor(sheet: sheets.Spreadsheet, sheetNum: Int = 0): DataSource, NoteDataSource {
     companion object {
         private val filterList = listOf(
             "szumma", "színes belépő"
         )
 
-        suspend fun new(url: String): GSheet {
+        suspend fun new(url: String, sheetNum: Int = 0): GSheet {
             if (!isSignedIn) throw Error("You must sign in to get gapi.sheets")
             val sheet = getSheetFromUrl(url)
             window.asDynamic().test = sheet
-            return GSheet(sheet)
+            return GSheet(sheet, sheetNum)
         }
 
         @NoLiveLiterals
@@ -36,7 +36,7 @@ class GSheet private constructor(sheet: sheets.Spreadsheet): DataSource, NoteDat
         }
     }
 
-    private val rows = sheet.sheets!![0].data!![0].rowData!!
+    private val rows = sheet.sheets!![sheetNum].data!![0].rowData!!
 
     private val principleIndexes = rows[0].values!!.mapIndexed { i, data ->
         (data.formattedValue?.trimAsPrinciple() ?: "") to i
diff --git a/src/jsMain/kotlin/ui/SheetsModal.kt b/src/jsMain/kotlin/ui/SheetsModal.kt
index 2377730..537931c 100644
--- a/src/jsMain/kotlin/ui/SheetsModal.kt
+++ b/src/jsMain/kotlin/ui/SheetsModal.kt
@@ -37,6 +37,7 @@ private object SheetsModal: ModalType {
         }
 
         var sheetUrl by remember { mutableStateOf("") }
+        var sheetNum by remember { mutableStateOf(1) }
         var sheet by remember { mutableStateOf<GSheet?>(null) }
 
         setHint(
@@ -86,13 +87,26 @@ private object SheetsModal: ModalType {
                 value(sheetUrl)
             })
         }
+        withLabel("Hányadik sheet (fül)") { id ->
+            Input(type = InputType.Number, attrs = {
+                id(id)
+                classes("uk-input")
+                onInput {
+                    sheetNum = it.value?.toInt() ?: return@onInput
+                    if (sheetNum <= 0) {
+                        sheetNum = 1
+                    }
+                }
+                value(sheetNum)
+            })
+        }
         Hr {  }
         divFloatRight {
             Button({
                 classes("uk-button", "uk-button-primary")
                 onClick {
                     scope.launch {
-                        sheet = GSheet.new(sheetUrl)
+                        sheet = GSheet.new(sheetUrl, sheetNum-1)
                         sheet?.process { setState(ImportInProgress(it)) }
                         setState(Ready)
                     }
-- 
GitLab