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à API Apps Script.
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 bị tắt, diễn ra vào hoặc sau ngày 31 tháng 1 năm 2026. Nếu có nhiều tập lệnh giống hệt nhau chạy trên Rhino, bạn có thể di chuyển tất cả các tập lệnh đó sang V8 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 về Apps Script.
- Nếu API đang tắt, hãy nhấp vào Google Apps Script API, sau đó bật nút bật/tắt Google Apps Script API.
- Tạo dự án Google Cloud 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 xin phép bằng OAuth.
Trong dự án trên Google Cloud, 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 Google Cloud.
- Nhấp vào Đặt dự án.
Di chuyển tập lệnh
Mã mẫu sau đây cho biết cách sử dụng API Apps Script để di chuyển các tập lệnh giống hệt 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 tập hợp các 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.
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!`);
}
});
}
Để sử dụng API Apps Script 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 biểu tượng 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"
}