Google Trang tính, một phần của Google Workspace, cho phép các nhóm tạo và chia sẻ bảng tính trực tuyến, đồng thời đã phát triển thành một công cụ kinh doanh được sử dụng rộng rãi. Đối với nhiều quy trình, tất cả những gì bạn cần là một bảng tính được chia sẻ và rô-bốt sử dụng nó
Bạn nên sử dụng thư viện tự động hóa nào?
RPA. Đám mây. Thư viện Google, một phần của RPA Framework, cho phép tương tác với Google Trang tính
Cài đặt và thiết lập
Rô-bốt của bạn sẽ cần xác thực với Google để có thể tương tác với bảng tính Google Trang tính, sử dụng khái niệm tài khoản dịch vụ. Sau đó, tài khoản được sử dụng bởi robot sẽ cần được thêm làm cộng tác viên vào [các] trang tính bạn muốn truy cập
Tạo tài khoản dịch vụ của Google
Để truy cập dữ liệu được lưu trữ trong Google Trang tính, bạn sẽ cần tạo một tài khoản dịch vụ và nhận một bộ thông tin đăng nhập OAuth2 từ Bảng điều khiển API của Google
- Truy cập Google APIs Console khi đã đăng nhập vào tài khoản Google của bạn
- Tạo một dự án mới và đặt tên cho nó
- Nhấp vào
ENABLE APIS AND SERVICES
- Tìm và kích hoạt
Google Sheet API
- Tạo thông tin đăng nhập mới cho
Google Sheets API
. Chọn
0 từ danh sách thả xuống và chọn*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
1. Sau đó bấm vào nút*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
2*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
- Trên màn hình tiếp theo, hãy chọn tên cho tài khoản dịch vụ của bạn, chỉ định vai trò của nó là
3->*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
4 và nhấp vào*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
5*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
- Tệp JSON thông tin đăng nhập sẽ được trình duyệt của bạn tải xuống
Tệp thông tin xác thực cho phép mọi người truy cập tài nguyên đám mây của bạn, vì vậy bạn nên lưu trữ tệp này một cách an toàn.
- Tìm tệp đã tải xuống và đổi tên thành
6*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
Tạo một Google Sheet mới và thêm Tài khoản dịch vụ làm trình chỉnh sửa cho nó
- Tạo hoặc chọn Google Trang tính hiện có
- Mở tệp
6 và tìm thuộc tính*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
8*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
- Nhấp vào nút
9 ở trên cùng bên phải và thêm địa chỉ email của tài khoản dịch vụ với tư cách là người chỉnh sửa*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
Nếu bạn chỉ muốn cho phép tài khoản có quyền đọc bảng tính, hãy chỉ định cho tài khoản đó vai trò
0*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json
- Lưu ý ID của tài liệu Google Trang tính, có trong URL của nó, sau phần tử
1. Vì vậy, ví dụ: nếu URL của tài liệu của bạn là*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json
2, thì ID sẽ là*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json
3*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json
ví dụ rô bốt
Bây giờ, việc thiết lập tài khoản của chúng ta đã hoàn tất, chúng ta sẽ xây dựng một rô-bốt
- Đọc dữ liệu hiện có từ Bảng tính Google và ghi lại dữ liệu đó
- Thêm nhiều dữ liệu hơn vào Google Sheet
Đây là bảng tính mẫu của chúng tôi với một số dữ liệu thử nghiệm
Tạo một robot mới và thêm RPA. Đám mây. thư viện Google
- Tạo rô bốt mới bằng tiện ích mở rộng VS Code Robocorp
- Chỉnh sửa tệp trong robot của bạn như thế này
channels:
- conda-forge
dependencies:
- python=3.9.13
- pip=22.1.2
- pip:
- rpaframework-google==6.0.0
Theo mặc định, gói
5 trong RPA Framework không được bao gồm do kích thước của các thành phần phụ thuộc. Bằng cách thêm dòng*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json
6, bạn đang thêm nó một cách rõ ràng vào rô-bốt của mình*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json
kịch bản robot
Quan trọng. Hãy nhớ thêm tệp
6 vào thư mục gốc của rô-bốt của bạn*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
*** Settings ***
Documentation An example robot that reads and writes data
.. into a Google Sheet document.
Library RPA.Cloud.Google
Suite Setup Init Sheets service_account.json
*** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
*** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
Add values to the Google Sheet
${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]]
Insert Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
.. ${values}
.. ROWS
Kịch bản robot giải thích
*** Settings ***
Documentation An example robot that reads and writes data
.. into a Google Sheet document.
Library RPA.Cloud.Google
Suite Setup Init Sheets service_account.json
Trong phần
*** Settings ***
Documentation An example robot that reads and writes data
.. into a Google Sheet document.
Library RPA.Cloud.Google
Suite Setup Init Sheets service_account.json
8, cài đặt *** Settings ***
Documentation An example robot that reads and writes data
.. into a Google Sheet document.
Library RPA.Cloud.Google
Suite Setup Init Sheets service_account.json
9 giải thích những gì rô-bốt của chúng tôi làm. Sau đó, chúng tôi thêm thư viện *** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
0. Cuối cùng, chúng tôi sử dụng cài đặt *** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
1 để khởi tạo ứng dụng khách Google Trang tính. Bằng cách này, nó sẽ chỉ được khởi tạo một lần, ngay cả khi robot của chúng tôi có nhiều nhiệm vụBạn có thể tìm hiểu thêm về Thiết lập Suite và Teardown trong phần
*** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
Trong phần
*** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
2, chúng tôi đặt hai biến
3 sẽ giữ id của tài liệu Google Trang tính của chúng tôi*** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10
4 là phạm vi ô mà chúng tôi muốn làm việc, được viết bằng. Trong trường hợp của chúng tôi, khu vực chúng tôi quan tâm trong bảng tính của chúng tôi bắt đầu từ ô*** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10
5 và kết thúc bằng ô*** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10
6 của trang tính đầu tiên, vì vậy giá trị của chúng tôi sẽ là*** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10
7*** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10
*** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
Trong nhiệm vụ này, chúng tôi đang đọc các hàng được chỉ định bởi
*** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
4 của Google Trang tính của chúng tôi, được xác định bởi *** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
3, vào biến *** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
0Từ khóa
*** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
1 trả về một từ điển có mục *** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
2 chứa danh sách các hàng. Để làm cho rô-bốt mạnh mẽ hơn, chúng tôi kiểm tra sự tồn tại của khóa *** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
2 trước khi truy cập nó [một trang tính trống sẽ khiến các giá trị bị thiếu]. Sử dụng từ khóa *** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
4, chúng tôi có thể ghi dữ liệu hàng đó*** Tasks ***
Add values to the Google Sheet
${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]]
Insert Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
.. ${values}
.. ROWS
Trong tác vụ này, chúng tôi thêm một số dữ liệu tùy ý vào một hàng mới trong bảng tính
- Sử dụng từ khóa
5, chúng tôi tạo một biến có các giá trị cho hàng*** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END
- Chúng tôi chuyển các giá trị, id trang tính và phạm vi cho từ khóa
6. Các giá trị sẽ được thêm vào hàng có sẵn đầu tiên bằng cách sử dụng tùy chọn thứ nguyên chính*** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END
7*** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END
Lưu trữ thông tin xác thực trong Control Room Vault
Bạn không bao giờ được đưa trực tiếp mật khẩu hoặc tệp thông tin xác thực vào mã rô-bốt của mình. Thay vì đọc thông tin đăng nhập từ tệp
*** Settings ***
Documentation An example robot that reads and writes data
.. into a Google Sheet document.
Library RPA.Cloud.Google
Suite Setup Init Sheets service_account.json
*** Variables ***
${SHEET_ID} 1234567890123abcf
${SHEET_RANGE} Sheet1!A2:D10
*** Tasks ***
Read values from the Google Sheet
${spreadsheet_content}= Get Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
IF "values" in ${spreadsheet_content}
Log Many ${spreadsheet_content["values"]}
END
Add values to the Google Sheet
${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]]
Insert Sheet Values
.. ${SHEET_ID}
.. ${SHEET_RANGE}
.. ${values}
.. ROWS
6, rô bốt của chúng tôi có thể sử dụng tính năng kho tiền của Phòng điều khiểnThiết lập rô-bốt của bạn để chạy trong Phòng điều khiển
Tạo một kho tiền mới trong không gian làm việc của rô-bốt. Đặt tên cho nó là
9*** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END
Tạo một bí mật mới trong kho tiền. Cung cấp cho nó một khóa của
0 và dán nội dung của tệp*** Tasks *** Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
6 vào trường giá trị*** Settings *** Documentation An example robot that reads and writes data .. into a Google Sheet document. Library RPA.Cloud.Google Suite Setup Init Sheets service_account.json *** Variables *** ${SHEET_ID} 1234567890123abcf ${SHEET_RANGE} Sheet1!A2:D10 *** Tasks *** Read values from the Google Sheet ${spreadsheet_content}= Get Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} IF "values" in ${spreadsheet_content} Log Many ${spreadsheet_content["values"]} END Add values to the Google Sheet ${values}= Evaluate [["Mark", "The Monkey", 100000, 10000]] Insert Sheet Values .. ${SHEET_ID} .. ${SHEET_RANGE} .. ${values} .. ROWS
Làm cách nào để viết vào trang tính Google bằng Java?
Khởi động nhanh Java .Trên trang nàymục tiêuđiều kiện tiên quyếtThiết lập môi trường của bạn. Kích hoạt API. Ủy quyền thông tin đăng nhập cho một ứng dụng máy tính để bànChuẩn bị không gian làm việcthiết lập mẫuChạy mẫuBước tiếp theoLàm cách nào để chèn dữ liệu vào Google Sheets bằng JavaScript?
$. ajax[{ url. 'https. // bảng tính. Google. com/feeds/spreadsheets/private/full?alt=json-in-script&access_token=' + access_token, dataType. 'JSONP', thành công. function[data]{ // sử dụng bảng tính } }];Bạn có thể viết mã trong Google Trang tính không?
Mặc dù Google Trang tính cung cấp nhiều chức năng cho mọi người, nhưng bạn cũng có thể viết mã gì đó phù hợp hơn với nhu cầu của mình . Và vì bạn có thể sử dụng lại mã nhiều lần, nó giúp bạn làm việc năng suất và hiệu quả hơn rất nhiều.