如何使用 Apps 脚本将文件附加到 Google 课堂“材料”作业?

问题描述 投票:0回答:1

使用 Google 表格中的应用程序脚本,我可以在 Google 课堂中为学生创建新的作业或“材料”,以编程方式从 Google 表格传递信息。

Google 表格中的一个字段 - 来自 Google 表单的回复 - 是作为证据上传的文件的链接。我想将该链接文件附加到 Google 课堂中的“材料”作业。

我有一个将所有其他信息传递给 Google Classroom 的功能,我确信文件本身正在被发现并传递给 Google 云端硬盘,但我的代码没有将文件附加到“材料”作业。问题似乎就在这里-

      Classroom.Courses.CourseWorkMaterials.create({
        assigneeMode: "INDIVIDUAL_STUDENTS",
        individualStudentsOptions: { studentIds: [studentId] },
        title: pupil + " - " + curriculumArea,
        description: materials.description, 
        materials: materials.materials 
      }, courseId);

错误信息是

Error   
GoogleJsonResponseException: API call to classroom.courses.courseWorkMaterials.create failed with error: Invalid JSON payload received. Unknown name "id" at 'course_work_material.materials[0].drive_file': Cannot find field.
Invalid JSON payload received. Unknown name "title" at 'course_work_material.materials[0].drive_file': Cannot find field.
Invalid JSON payload received. Unknown name "driveFileId" at 'course_work_material.materials[1].drive_file': Cannot find field.
Invalid JSON payload received. Unknown name "title" at 'course_work_material.materials[1].drive_file': Cannot find field.
createMaterialsFromGoogleSheet  

我认为是指

    // Define the materials array
    var materials = {
      title: pupil + " - " + curriculumArea,
      description: "Learning intentions: " + learningIntentions + "\n\nSuccess criteria: " + successCriteria + "\n\nAssessment comments: " + assessmentComments + "\n\nNext steps: " + nextSteps,
      materials: [
      {
        driveFile: {
          id: evidenceId,
          title: fileName
        }
      }
    ]
    };

      // Add the evidence file to the materials
materials.materials.push({
  driveFile: {
    id: evidenceId,
    title: fileName
  }
});

这就是这些片段如何融入功能

 // Loop through each row of data and create a new material for each student
  for (var i = 1; i < data.length; i++) {
    var pupil = data[i][1];
    var curriculumArea = data[i][2];
    var learningIntentions = data[i][3];
    var successCriteria = data[i][5];
    var assessmentComments = data[i][6];
    var nextSteps = data[i][7];
    var evidenceLink = data[i][8];

    // Define the materials array
    var materials = {
      title: pupil + " - " + curriculumArea,
      description: "Learning intentions: " + learningIntentions + "\n\nSuccess criteria: " + successCriteria + "\n\nAssessment comments: " + assessmentComments + "\n\nNext steps: " + nextSteps,
      materials: [
      {
        driveFile: {
          id: evidenceId,
          title: fileName
        }
      }
    ]
    };

    var evidenceId = "";
    var fileName = "";
    if (evidenceLink) {
      evidenceId = uploadFileToDrive(evidenceLink);
      if (!evidenceId) {
        Logger.log("Could not upload file: " + evidenceLink);
        continue;
      }
      
      // Get the file name from the evidenceLink
      var fileName = evidenceLink.split("/").pop();
      
      // Add the evidence file to the materials
materials.materials.push({
  driveFile: {
    id: evidenceId,
    title: fileName
  }
});
    
      // Add the evidence link to the description
      assessmentComments += "\n\nEvidence: " + evidenceLink;
    }


    if (pupil) {
      var studentId = getStudentId(pupil, courseId);
      if (!studentId) {
        Logger.log("Could not find student with name '" + pupil + "' in course. Skipping row.");
        continue;
      }
      
      Classroom.Courses.CourseWorkMaterials.create({
        assigneeMode: "INDIVIDUAL_STUDENTS",
        individualStudentsOptions: { studentIds: [studentId] },
        title: pupil + " - " + curriculumArea,
        description: materials.description, 
        materials: materials.materials 
      }, courseId);
    }
  }
} 

看起来错误与驱动文件有关。在谷歌表格中它看起来像这样 - https://drive.google.com/open?id=18xxMxkYp5W5x3NbbMAqVnJOx6pwlxxxx

evidenceId = uploadFileToDrive(evidenceLink)调用的代码;是

function uploadFileToDrive(url) {
  var response = UrlFetchApp.fetch(url, {
    headers: {
      Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
    }
  });
  var file = Drive.Files.insert({
    title: "evidence",
    mimeType: response.getHeaders()['Content-Type']
  }, response.getBlob());
  return file.id;
}
google-apps-script google-sheets google-drive-api google-classroom
1个回答
0
投票

修改点:

  • 在您的脚本中,在以下部分中,未声明

    evidenceId
    fileName
    。我认为这就是第一个
    Unknown name "title" at 
    .

    的原因
      var materials = {
        title: pupil + " - " + curriculumArea,
        description: "Learning intentions: " + learningIntentions + "\n\nSuccess criteria: " + successCriteria + "\n\nAssessment comments: " + assessmentComments + "\n\nNext steps: " + nextSteps,
        materials: [
        {
          driveFile: {
            id: evidenceId,
            title: fileName
          }
        }
      ]
      }
    
  • 看来

    {driveFile: {id: evidenceId, title: fileName}}
    应该是
    {driveFile: {driveFile: {id: evidenceId, title: fileName}}}
    。我认为这就是第二个
    Unknown name "title" at 
    .

    的原因

当这些要点在你的剧本中体现出来,就变成了这样

修改脚本:

  for (var i = 1; i < data.length; i++) {
    var pupil = data[i][1];
    var curriculumArea = data[i][2];
    var learningIntentions = data[i][3];
    var successCriteria = data[i][5];
    var assessmentComments = data[i][6];
    var nextSteps = data[i][7];
    var evidenceLink = data[i][8];

    // Define the materials array
    var materials = {
      title: pupil + " - " + curriculumArea,
      description: "Learning intentions: " + learningIntentions + "\n\nSuccess criteria: " + successCriteria + "\n\nAssessment comments: " + assessmentComments + "\n\nNext steps: " + nextSteps,
      materials: []
    };

    var evidenceId = "";
    var fileName = "";
    if (evidenceLink) {
      evidenceId = uploadFileToDrive(evidenceLink);
      if (!evidenceId) {
        Logger.log("Could not upload file: " + evidenceLink);
        continue;
      }

      // Get the file name from the evidenceLink
      var fileName = evidenceLink.split("/").pop();

      // Add the evidence file to the materials
      materials.materials.push({
        driveFile: {
          driveFile: {
            id: evidenceId,
            title: fileName
          }
        }
      });

      // Add the evidence link to the description
      assessmentComments += "\n\nEvidence: " + evidenceLink;
    }

    if (pupil) {
      var studentId = = getStudentId(pupil, courseId);
      if (!studentId) {
        Logger.log("Could not find student with name '" + pupil + "' in course. Skipping row.");
        continue;
      }

      Classroom.Courses.CourseWorkMaterials.create({
        assigneeMode: "INDIVIDUAL_STUDENTS",
        individualStudentsOptions: { studentIds: [studentId] },
        title: pupil + " - " + curriculumArea,
        description: materials.description,
        materials: materials.materials
      }, courseId);
    }
  }

注:

  • 在这个修改中,它假设你使用请求体的所有值都是有效值。如果包含无效值,我担心会发生另一个错误。请注意这一点。
© www.soinside.com 2019 - 2024. All rights reserved.