英文:
Google Classroom assignment attachment links in wrong format - HTML instead of jpg or pdf
问题
以下是您的代码部分的中文翻译:
function uploadFileToDrive(url) {
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
});
var contentType = response.getHeaders()['Content-Type'];
var fileExtension = url.substring(url.lastIndexOf('.') + 1);
var mimeType;
switch (fileExtension) {
case 'jpg':
mimeType = 'image/jpg';
break;
case 'pdf':
mimeType = 'application/pdf';
break;
// 需要时,可以添加更多文件类型的情况
default:
mimeType = contentType;
break;
}
var file = Drive.Files.insert({
title: "evidence",
mimeType: mimeType
}, response.getBlob());
return file.id;
}
希望这对您有所帮助。如果您有其他翻译需求,请随时告诉我。
英文:
I have a google sheet posting materials assignments to Google classroom. These include 'evidence' image and pdf attachments. When the material is posted the attachments are linked as HTML. The code seems to be converting the image files (jpg) to HTML.
This is the upload function. I tried adding in different file extensions but this made no difference.
function uploadFileToDrive(url) {
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
});
var contentType = response.getHeaders()['Content-Type'];
var fileExtension = url.substring(url.lastIndexOf('.') + 1);
var mimeType;
switch (fileExtension) {
case 'jpg':
mimeType = 'image/jpg';
break;
case 'pdf':
mimeType = 'application/pdf';
break;
// Add more cases for other file types as needed
default:
mimeType = contentType;
break;
}
var file = Drive.Files.insert({
title: "evidence",
mimeType: mimeType
}, response.getBlob());
return file.id;
}
答案1
得分: 1
从你的问题来看,如果你使用 https://drive.google.com/open?id=###spreadsheetId###
作为 url
的值,那么在这种情况下,我认为使用你显示的脚本创建的文件是登录界面的HTML数据。我认为这可能是你目前问题的原因。
如果你想要将Google电子表格导出为PDF文件,可以考虑以下修改:
修改后的脚本:
在这种情况下,请使用文件ID。如果你使用的是像 https://drive.google.com/open?id=###spreadsheetId###
这样的URL,###spreadsheetId###
就是文件ID。
function uploadFileToDrive(fileId) {
var blob = DriveApp.getFileById(fileId).getBlob();
var file = Drive.Files.insert({ title: "evidence" }, blob);
return file.id;
}
- 对于Google Docs文件(文档,电子表格,幻灯片),当从文件中检索blob时,mimeType会自动转换为PDF格式。我认为这种情况是可行的。
添加:
根据你的后续回复,
> 运行这个脚本时出现了“在对象DriveApp上获取getFileById方法或属性时出现意外错误”的错误。 uploadFileToDrive @ var blob = DriveApp.getFileById(fileId).getBlob(); createMaterialsFromGoogleSheet @ evidenceId = uploadFileToDrive(evidenceLink);
> 这是不是前进了一步?function uploadFileToDrive(fileId) { var blob = Drive.Files.get(fileId).getBlob(); var fileMetadata = { title: file.title }; var uploadedFile = Drive.Files.insert(fileMetadata, blob); return uploadedFile.id; } 出现了这个错误 - GoogleJsonResponseException:API调用到drive.files.get失败,错误为:找不到文件:drive.google.com/open?id=18exMxxxxxxxxxNbbMAqVnJOxxxxxxxxx - 我是文件的所有者,可以在浏览器中查看它。
在我最初的答案中,我提到了 在这种情况下,请使用文件ID。如果你使用的是URL,比如 https://drive.google.com/open?id=###spreadsheetId###,###spreadsheetId### 就是文件ID
。但根据你的回复,如果我理解正确,很遗憾,我担心你仍然在使用URL。因为当给我的脚本提供无效的文件ID时,会出现相同的错误。如果我理解正确,请使用文件ID而不是URL,并再次测试。
如果你需要使用类似 https://drive.google.com/open?id=###spreadsheetId###
的URL,请按以下方式修改上述脚本:
function uploadFileToDrive(url) {
var fileId = url.split("=")[1].split("&")[0];
var blob = DriveApp.getFileById(fileId).getBlob();
var file = Drive.Files.insert({ title: "evidence" }, blob);
return file.id;
}
-
但是,如果URL与
https://drive.google.com/open?id=###spreadsheetId###
不同,将会出现错误。请注意这一点。 -
如果你直接使用文件ID,请使用我的第一个建议的脚本。请注意这一点。
英文:
As just my guess, from your previous question, if you are using https://drive.google.com/open?id=###spreadsheetId###
as the value of url
, in this case, I think that the created file using your showing script is an HTML data of the login screen. I thought that this might be the reason for your current issue.
If you want to export Google Spreadsheet as a PDF file, how about the following modification?
Modified script:
In this case, please use the file ID. If you are using a URL like https://drive.google.com/open?id=###spreadsheetId###
, ###spreadsheetId###
is the file ID.
function uploadFileToDrive(fileId) {
var blob = DriveApp.getFileById(fileId).getBlob();
var file = Drive.Files.insert({ title: "evidence" }, blob);
return file.id;
}
- In the case of Google Docs files (Document, Spreadsheet, Slides), when the blob is retrieved from the file, the mimeType is automatically converted to a PDF format. I thought that this situation can be used.
Added:
From your following reply,
> running this gives Unexpected error while getting the method or property getFileById on object DriveApp. uploadFileToDrive @ var blob = DriveApp.getFileById(fileId).getBlob(); createMaterialsFromGoogleSheet @ evidenceId = uploadFileToDrive(evidenceLink);
> Is this a step forward? function uploadFileToDrive(fileId) { var blob = Drive.Files.get(fileId).getBlob(); var fileMetadata = { title: file.title }; var uploadedFile = Drive.Files.insert(fileMetadata, blob); return uploadedFile.id; } gives this error - GoogleJsonResponseException: API call to drive.files.get failed with error: File not found: drive.google.com/open?id=18exMxxxxxxxxxNbbMAqVnJOxxxxxxxxx - I am the owner of the file and can view it in a browser.
In my initial answer, I have mentioned In this case, please use the file ID. If you are using a URL like https://drive.google.com/open?id=###spreadsheetId###, ###spreadsheetId### is the file ID.
. But, from your reply, if my understanding is correct, unfortunately, I'm worried that you have still used an URL. Because, when the invalid file ID is given to my script, the same error occurs. If my understanding is correct, please use the file ID instead of the URL, and test it again.
If you are required to use a URL like https://drive.google.com/open?id=###spreadsheetId###
, please modify the above script as follows.
function uploadFileToDrive(url) {
var fileId = url.split("=")[1].split("&")[0];
var blob = DriveApp.getFileById(fileId).getBlob();
var file = Drive.Files.insert({ title: "evidence" }, blob);
return file.id;
}
-
But, if the URL is different from
https://drive.google.com/open?id=###spreadsheetId###
, an error occurs. Please be careful about this. -
And, if you directly use the file ID, please use my first suggested script. Please be careful about this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论