Pygsheets adalah pustaka python sederhana yang dapat digunakan untuk mengotomatiskan Google Sheets melalui Google Sheets API. Contoh penggunaan pustaka ini adalah untuk mengotomatiskan pembuatan plot grafik berdasarkan beberapa data dalam file CSV yang dapat kami ekspor ke Google Sheets. Jadi, mari kita lanjutkan lebih jauh
Bagaimana cara kerja pygsheets dengan Google Sheets?
- Klien membuat dan mengakses spreadsheet
- Spreadsheet adalah kelas yang digunakan untuk mewakili Google Sheet
- Setiap spreadsheet memiliki beberapa 'Lembar Kerja'
- Data di dalam lembar kerja dapat diakses melalui objek 'Sel' yang mendukung pemformatan, rumus, dll
Langkah 1. Aktifkan API untuk Google Spreadsheet dan Google Drive
A. Buka Google Developers Console dan buat proyek baru.
B. Masukkan nama untuk proyek. Anda dapat meninggalkan 'Location' sebagai 'No Organization'. Tekan Buat
C. Sekarang, kami akan mengaktifkan Google Sheets dan Google Drive API. Untuk melakukannya, klik 'AKTIFKAN API DAN LAYANAN'.
D. Sekarang masukkan 'Google Sheets API' di bilah pencarian, klik opsi 'Google Sheets API', dan tekan 'AKTIFKAN'
E. Demikian pula, cari dan aktifkan 'Google Drive API'
Langkah 2. Buat Akun Layanan dan ambil kredensial
Akun Layanan adalah apa yang akan kami gunakan untuk membuat panggilan API kami untuk mengedit spreadsheet. Itu terkait dengan id email dari Akun Pengembang Google yang kami gunakan untuk membuat proyek kami
A. Untuk membuatnya, klik 'BUAT KREDENSIAL'
B. Pilih 'Google Sheets API' di bagian 'Pilih API', 'Data Aplikasi' dan 'Tidak, saya tidak menggunakannya' dalam pertanyaan berikut. Klik 'BERIKUTNYA'
C. Masukkan nama tampilan dan nama untuk akun layanan
D. Sekarang kita akan memasuki tingkat akses untuk akun layanan. Klik 'Pilih Peran' dan pilih 'Editor' di bawah bagian 'dasar'. Tekan 'LANJUTKAN'
E. Anda dapat mengosongkan bagian 'Beri pengguna akses ke akun layanan ini' dan sekarang, tekan 'SELESAI'
F. Sekarang klik pada akun layanan yang baru saja dibuat
G. Buka bagian 'KUNCI' dan klik 'TAMBAHKAN KUNCI'. Pilih 'Buat kunci baru'
H. Pilih 'JSON' untuk mengunduh kunci dalam format file JSON. Klik 'BUAT'
I. File di atas akan diunduh ke sistem Anda. Jangan berbagi kunci dengan siapa pun dan jaga keamanannya. Tempatkan file di folder tempat Anda ingin membuat proyek python Anda
Langkah 3. Tambahkan Akun Layanan sebagai editor
Buat Lembar Google baru di akun Google Anda, atau Anda bahkan dapat menggunakan yang sudah ada. Cukup tambahkan akun layanan sebagai salah satu editor di sheet tersebut. Anda akan menemukan email Akun Layanan di file kunci Anda sebagai "client_email"
Langkah 4. Otorisasi pygsheets
A. Sekarang buat utama. py dan letakkan di folder yang sama dengan file Keys Anda
B. Instal Perpustakaan yang Diperlukan
pip install pygsheets
C. Sekarang, tulis kode berikut di main. py untuk memverifikasi keberhasilan otorisasi pygsheets
Python3
# Importing required library
_
import
pygsheets
_
# Create the Client
# Enter the name of the downloaded KEYS
_
# file in service_account_file
client
=
# Importing required library
0=
# Importing required library
2# Importing required library
3
_
# Importing required library
_5
# Importing required library
_6
# Importing required library
_7
# Importing required library
_8
# Importing required library
_9
import
0import
1
Kode di atas harus mencetak nama spreadsheet yang kami bagikan dengan akun layanan di langkah 3. Karenanya, kami telah berhasil menautkan Google Sheet kami dengan Akun Layanan dan sekarang, kami akan menggunakan Klien untuk mengedit spreadsheet
Langkah 5. Mengedit spreadsheet
A. Sekarang, kita akan melihat beberapa perintah untuk mengedit spreadsheet kita melalui kode Python
Python3
# Importing required library
_
import
pygsheets
_
# Create the Client
client
=
# Importing required library
0=
# Importing required library
2# Importing required library
3
_
pygsheets
_4
pygsheets
5=
pygsheets
7pygsheets
8pygsheets
9
0# Importing required library
3
_
_3
4=
6
7
8
9# Importing required library
3
_
# Create the Client
2
_
# Create the Client
_4
# Create the Client
5# Create the Client
6# Create the Client
7# Create the Client
8
8# Enter the name of the downloaded KEYS
0# Enter the name of the downloaded KEYS
1=
# Enter the name of the downloaded KEYS
3
_
# Enter the name of the downloaded KEYS
_5
# Enter the name of the downloaded KEYS
_6
# Enter the name of the downloaded KEYS
7# Enter the name of the downloaded KEYS
8# Enter the name of the downloaded KEYS
9# file in service_account_file
0# file in service_account_file
1# file in service_account_file
2# file in service_account_file
3
# file in service_account_file
4# file in service_account_file
5# file in service_account_file
6# file in service_account_file
1# file in service_account_file
8# file in service_account_file
3
# file in service_account_file
4# file in service_account_file
5client
2client
3client
4
_
client
_6
# Create the Client
5client
8# Create the Client
7# Create the Client
8
8# Enter the name of the downloaded KEYS
0# Enter the name of the downloaded KEYS
1=
=
5
# Enter the name of the downloaded KEYS
7=
7# Enter the name of the downloaded KEYS
9=
9# file in service_account_file
1# Importing required library
01# file in service_account_file
1=
9# file in service_account_file
1# Importing required library
05________7______1________1______1_______1_7_______8
_
# Importing required library
_10
# Importing required library
11# Importing required library
12
8# Importing required library
14# Importing required library
15# Importing required library
16
8# Importing required library
18# Importing required library
19# Importing required library
20________1______3