google-apps-script - 是否可以在 AppScript 中批量请求谷歌教室
问题描述
我正在开发一个使用 AppScript 将数据上传到 Google Classroom 的应用程序。我经常使用两种方法:
/*
*@description adding grade to the submission
*@param courseId {string} id of clasroom in which there is coursework that we are intrested in
*@param courseWorkId {string} id of coursework in which there is submission that we are intrested in
*@param submId {string} submission id of submission to which we want to set mark*/
function setMark(courseId, courseWorkId, submIdd, mark) {
var maark = parseFloat(mark) || 0
if (maark < 0) {
maark = 0
}
var studentSubmissionRes = {
'assignedGrade': maark,
'draftGrade': maark
}
var extra = {
'updateMask': 'assignedGrade,draftGrade'
};
Classroom.Courses.CourseWork.StudentSubmissions.patch(studentSubmissionRes,
courseId, courseWorkId, submIdd, extra)
}
/**
*@description addind link to the submission
*@param fileUrl {string} url of file we want to add to submission
*@param courseId {string} id of clasroom in which there is coursework that we are intrested in
*@param courseWorkId {string} id of coursework in which there is submission that we are intrested in
*@param submId {string} submission id of submission to which we want to add the link
*/
function setLinkTosubmission(fileUrl, courseId, courseWorkId, submId) {
var request = ModifyAttachmentsRequest = {
'addAttachments': [{
'link': {
'url': fileUrl
}
}]
}
Classroom.Courses.CourseWork.StudentSubmissions.modifyAttachments(request, courseId, courseWorkId, submId)
}
它们都可以工作,但我想提高性能和批量请求以添加链接或评分,这在 Google Sheets API 中是可能的,但我找不到这样做的方法。
另外,是否有可能一次批量更新多个文档中的文本?
解决方案
课堂高级服务不支持批处理请求。出于这个原因,您必须在没有支持的情况下实现创建批处理请求正文的代码,然后使用UrlFetchApp发送它。见下文:
例子
function main() {
var body = [
{
method: "PATCH",
endpoint: "/v1/courses/134529639?updateMask=name",
requestBody: {"name": "Course 1"}
},
{
method: "PATCH",
endpoint: "/v1/courses/134529901?updateMask=section",
requestBody: {"section": "Section 2"}
}
];
var url = "https://classroom.googleapis.com/batch";
var boundary = "batch_foobarbaz";
var contentId = 0;
var data = "--" + boundary + "\r\n";
for (var i in body) {
data += "Content-Type: application/http\r\n";
data += "Content-ID: " + ++contentId + "\r\n\r\n";
data += body[i].method + " " + body[i].endpoint + "\r\n";
data += body[i].requestBody ? "Content-Type: application/json; charset=utf-8\r\n\r\n" : "\r\n";
data += body[i].requestBody ? JSON.stringify(body[i].requestBody) + "\r\n" : "";
data += "--" + boundary + "\r\n";
}
var payload = Utilities.newBlob(data).getBytes();
var options = {
method: "post",
contentType: "multipart/mixed; boundary=" + boundary,
payload: payload,
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
muteHttpExceptions: true,
};
var res = UrlFetchApp.fetch(url, options).getContentText();
Logger.log(res);
}
在这种情况下,将创建一个批处理请求,其中包含PATCH
对 API 的两个不同调用。获得的响应将类似于以下内容:
HTTP/1.1 200
Content-Length: response_total_content_length
Content-Type: multipart/mixed; boundary=batch_foobarbaz
--batch_foobarbaz
Content-Type: application/http
Content-ID: <response-item1:12930812@classroom.example.com>
HTTP/1.1 200 OK
Content-Type application/json
Content-Length: response_part_1_content_length
{
"id": "134529639",
"name": "Course 1",
"section": "Section 1",
"ownerId": "116269102540619633451",
"creationTime": "2015-06-25T14:23:56.535Z",
"updateTime": "2015-06-25T14:33:06.583Z",
"enrollmentCode": "6paeflo",
"courseState": "PROVISIONED",
"alternateLink": "http://classroom.google.com/c/MTM0NTI5NjM5"
}
--batch_foobarbaz
Content-Type: application/http
Content-ID: <response-item2:12930812@classroom.example.com>
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: response_part_2_content_length
{
"id": "134529901",
"name": "Course 1",
"section": "Section 2",
"ownerId": "116269102540619633451",
"creationTime": "2015-06-25T14:23:08.761Z",
"updateTime": "2015-06-25T14:33:06.490Z",
"enrollmentCode": "so75ha5",
"courseState": "PROVISIONED",
"alternateLink": "http://classroom.google.com/c/MTM0NTI5OTAx"
}
--batch_foobarbaz--
参考
推荐阅读
- android - gradle 没有构建,项目也没有在 android View 中打开
- sql - 将 varchar 值“N”转换为数据类型 smallint 时转换失败
- asp.net-core - 根据子域切换appsettings.json
- c - Windows 共享驱动器未使用 C Windows API 在 GetLogicalDriveStrings() 中获取完整列表
- build-system - 寻找满足多个标准的构建系统
- azure - Azure 环境变量如何覆盖 web.config 值?
- python-3.x - cog 文件中的所有内容似乎都是正确的,但不起作用
- openapi - openapi 3:如何指定标头参数为 JSON
- java - 用于多个(动态)SAP 系统和存储库的 SAP JCo 服务器
- java - Spring无法将@Service bean注入@Autowired注释字段