Trang này mô tả cách di chuyển các tập lệnh giống hệt nhau sang V8 bằng Apps Script và Apps Script API.
Bạn phải di chuyển mọi tập lệnh sử dụng môi trường thời gian chạy Rhino trước khi Rhino ngừng hoạt động (từ ngày 31 tháng 1 năm 2026). Nếu có nhiều tập lệnh giống hệt nhau đang chạy trên Rhino, bạn có thể di chuyển tất cả tập lệnh đó sang V8 cùng một lúc bằng cách sử dụng API Apps Script.
Thiết lập môi trường
- Trong phần cài đặt trang tổng quan Apps Script, hãy bật Apps Script API.
- Chuyển đến phần Cài đặt trang tổng quan Apps Script.
- Nếu API này đang tắt, hãy nhấp vào API Google Apps Script, sau đó bật nút bật/tắt API Google Apps Script.
- Tạo một dự án Google Cloud tiêu chuẩn hoặc sử dụng lại một dự án hiện có.
- Trong dự án trên Cloud, hãy định cấu hình màn hình đồng ý OAuth.
Trong dự án Cloud của bạn, hãy bật API Apps Script.
Tạo một dự án Apps Script và chỉ định dự án Apps Script cho dự án trên Cloud.
- Tạo một dự án Apps Script độc lập từ trang tổng quan Apps Script hoặc bằng cách truy cập vào script.new.
- Nhấp vào Project Settings (Cài đặt dự án)
.
- Trong mục Dự án trên Google Cloud Platform (GCP), hãy nhấp vào Thay đổi dự án.
- Nhập số dự án của dự án trên Cloud.
- Nhấp vào Đặt dự án.
Di chuyển tập lệnh
Mẫu mã sau đây cho biết cách sử dụng Apps Script API để di chuyển các tập lệnh giống nhau từ Rhino sang V8 bằng cách thay thế các tệp trong mỗi dự án Apps Script bằng một nhóm tệp tương thích với V8.
Đảm bảo bạn có ít nhất quyền chỉnh sửa đối với các dự án tập lệnh mà bạn dự định di chuyển.
Code.gs
function updateRhinoScripts() {
// An array of script IDs of script projects to migrate.
// TODO(developer): Replace with your script IDs.
const scriptIds = ['abcdef12345678', 'abcdef12345678'];
// An array of file objects to replace the existing files in each script project.
// Remember to include all files for the script, excluded files are deleted.
// TODO(developer): Replace with your script files.
const filesToUpdate = {
"files": [
{
"name": "Code",
"type": "SERVER_JS",
"source": "// New updates\nfunction myFunction() {\n console.log('Hello, world!');\n}"
},
{
"name": "appsscript",
"type": "JSON",
"source": JSON.stringify({
"timeZone": "America/New_York",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
})
}
]
};
updateMultipleAppsScripts(scriptIds, filesToUpdate);
}
function updateMultipleAppsScripts(scriptIds, filesToUpdate) {
// 'scriptIds' should be an array of script IDs
// 'filesToUpdate' should be an array of objects, each with:
// name: The filename (For example, "Code", "Utilities")
// source: The source code for that file.
scriptIds.forEach(function (scriptId) {
// Makes the API request.
const response = UrlFetchApp.fetch(
`https://script.googleapis.com/v1/projects/${scriptId}/content`,
{
method: "PUT",
headers: {
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`
},
contentType: "application/json",
payload: JSON.stringify(filesToUpdate),
muteHttpExceptions: true
}
);
if (response.getResponseCode() !== 200) {
console.log(`Error updating script ${scriptId}: ${response.getContentText()}`);
} else {
console.log(`Script ${scriptId} updated successfully!`);
}
});
}
appsscript.json
Để sử dụng Apps Script API trong dự án Apps Script, bạn phải thêm các phạm vi OAuth sau vào tệp kê khai:
"https://www.googleapis.com/auth/script.projects"
"https://www.googleapis.com/auth/script.external_request"
Để hiển thị tệp kê khai trong trình chỉnh sửa, hãy nhấp vào Project Settings (Cài đặt dự án) rồi đánh dấu vào hộp Show "appsscript.json" manifest file in editor (Hiển thị tệp kê khai "appsscript.json" trong trình chỉnh sửa). Sau đây là tệp kê khai mẫu có các phạm vi OAuth thích hợp:
{
"timeZone": "America/Denver",
"dependencies": {
},
"oauthScopes": [
"https://www.googleapis.com/auth/script.projects",
"https://www.googleapis.com/auth/script.external_request"
],
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}