Update app.py
Browse files
app.py
CHANGED
@@ -17,27 +17,33 @@ from llama_index.core.schema import Document
|
|
17 |
# ===================================
|
18 |
def read_google_sheets():
|
19 |
try:
|
|
|
20 |
scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
|
|
|
|
|
21 |
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
22 |
client = gspread.authorize(creds)
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
for sheet_name in
|
31 |
try:
|
32 |
-
sheet =
|
33 |
data = sheet.get_all_values()
|
34 |
-
formatted_text =
|
35 |
-
|
36 |
-
combined_text += formatted_text + "\n"
|
37 |
except gspread.exceptions.WorksheetNotFound:
|
38 |
-
|
39 |
-
|
40 |
-
return
|
|
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
return f"❌ ERROR: {str(e)}"
|
43 |
|
|
|
17 |
# ===================================
|
18 |
def read_google_sheets():
|
19 |
try:
|
20 |
+
# Tentukan scope akses ke Google Sheets & Drive
|
21 |
scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
|
22 |
+
|
23 |
+
# Load kredensial dari file credentials.json
|
24 |
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
25 |
client = gspread.authorize(creds)
|
26 |
|
27 |
+
# 📌 Gunakan ID Spreadsheet (satu file dengan banyak worksheet)
|
28 |
+
SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg" # 🔹 Ganti dengan ID spreadsheet Anda
|
29 |
+
SHEET_NAMES = ["datatarget", "cuti", "absen"] # 🔹 Nama worksheet yang ingin dibaca
|
30 |
+
|
31 |
+
all_data = ""
|
32 |
+
|
33 |
+
for sheet_name in SHEET_NAMES:
|
34 |
try:
|
35 |
+
sheet = client.open_by_key(SPREADSHEET_ID).worksheet(sheet_name)
|
36 |
data = sheet.get_all_values()
|
37 |
+
formatted_text = "\n".join([" | ".join(row) for row in data])
|
38 |
+
all_data += f"\n--- Data dari {sheet_name} ---\n" + formatted_text + "\n"
|
|
|
39 |
except gspread.exceptions.WorksheetNotFound:
|
40 |
+
all_data += f"\n❌ ERROR: Worksheet {sheet_name} tidak ditemukan!\n"
|
41 |
+
|
42 |
+
return all_data.strip()
|
43 |
+
|
44 |
+
except gspread.exceptions.SpreadsheetNotFound:
|
45 |
+
return "❌ ERROR: Spreadsheet tidak ditemukan. Pastikan ID/nama benar!"
|
46 |
+
|
47 |
except Exception as e:
|
48 |
return f"❌ ERROR: {str(e)}"
|
49 |
|