Google apps script 如何使用应用程序脚本通过Docs加载项将文件添加到Google Drive?
这是我的设想。我为谷歌文档创建了一个附加组件,充当视频工具箱 我尝试添加的一个功能是使用内置的网络摄像头(使用videojs录像机)录制视频,然后在文档中链接到该视频。我已经完成了视频部分的工作,但不确定如何将webm JS Blob转换为Google Blob,以便我可以在用户Google Drive上创建一个用于共享和链接的文件 只是想弄清楚这是怎么回事,这是我到目前为止所做的,没有任何运气 客户端代码Google apps script 如何使用应用程序脚本通过Docs加载项将文件添加到Google Drive?,google-apps-script,google-drive-api,google-docs,google-docs-api,Google Apps Script,Google Drive Api,Google Docs,Google Docs Api,这是我的设想。我为谷歌文档创建了一个附加组件,充当视频工具箱 我尝试添加的一个功能是使用内置的网络摄像头(使用videojs录像机)录制视频,然后在文档中链接到该视频。我已经完成了视频部分的工作,但不确定如何将webm JS Blob转换为Google Blob,以便我可以在用户Google Drive上创建一个用于共享和链接的文件 只是想弄清楚这是怎么回事,这是我到目前为止所做的,没有任何运气 客户端代码 //event handler for video recording finish
//event handler for video recording finish
vidrecorder.on('finishRecord', function()
{
// the blob object contains the recorded data that
// can be downloaded by the user, stored on server etc.
console.log('finished recording: ', vidrecorder.recordedData);
google.script.run.withSuccessHandler(function(){
console.log("winning");
}).saveBlob(vidrecorder.recordedData);
});
function saveBlob(blob) {
Logger.log("Uploaded %s of type %s and size %s.",
blob.name,
blob.size,
blob.type);
}
服务器端代码
//event handler for video recording finish
vidrecorder.on('finishRecord', function()
{
// the blob object contains the recorded data that
// can be downloaded by the user, stored on server etc.
console.log('finished recording: ', vidrecorder.recordedData);
google.script.run.withSuccessHandler(function(){
console.log("winning");
}).saveBlob(vidrecorder.recordedData);
});
function saveBlob(blob) {
Logger.log("Uploaded %s of type %s and size %s.",
blob.name,
blob.size,
blob.type);
}
我得到的错误似乎与blob的序列化有关。但实际上,这些异常并不是很有用——只是指向一些最小化的代码
编辑:请注意,这里没有涉及表单对象,因此没有表单帖子,也没有文件上传对象,正如其他人所指出的那样,但是它有点不同,因为我们得到了一个Blob对象,需要将其保存到服务器。感谢Zig Mandel和Steve Webster,他们从服务器提供了一些关于这方面的见解 我终于拼凑了足够的碎片来让它工作 客户端代码
vidrecorder.on('finishRecord', function()
{
// the blob object contains the recorded data that
// can be downloaded by the user, stored on server etc.
console.log('finished recording: ', vidrecorder.recordedData.video);
var blob = vidrecorder.recordedData.video;
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
b64Blob = reader.result;
google.script.run.withSuccessHandler(function(state){
console.log("winning: ", state);
}).saveB64Blob(b64Blob);
};
});
服务器代码
function saveB64Blob(b64Blob) {
var success = { success: false, url: null};
Logger.log("Got blob: %s", b64Blob);
try {
var blob = dataURItoBlob(b64Blob);
Logger.log("GBlob: %s", blob);
var file = DriveApp.createFile(blob);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.COMMENT);
success = { success: true, url: file.getUrl() };
} catch (error) {
Logger.log("Error: %s", error);
}
return success;
}
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = Utilities.base64Decode(dataURI.split(',')[1]);
else
byteString = decodeURI(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
return Utilities.newBlob(byteString, mimeString, "video.webm");
}
感谢Zig Mandel和Steve Webster,他们提供了有关这方面的一些见解 我终于拼凑了足够的碎片来让它工作 客户端代码
vidrecorder.on('finishRecord', function()
{
// the blob object contains the recorded data that
// can be downloaded by the user, stored on server etc.
console.log('finished recording: ', vidrecorder.recordedData.video);
var blob = vidrecorder.recordedData.video;
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
b64Blob = reader.result;
google.script.run.withSuccessHandler(function(state){
console.log("winning: ", state);
}).saveB64Blob(b64Blob);
};
});
服务器代码
function saveB64Blob(b64Blob) {
var success = { success: false, url: null};
Logger.log("Got blob: %s", b64Blob);
try {
var blob = dataURItoBlob(b64Blob);
Logger.log("GBlob: %s", blob);
var file = DriveApp.createFile(blob);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.COMMENT);
success = { success: true, url: file.getUrl() };
} catch (error) {
Logger.log("Error: %s", error);
}
return success;
}
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = Utilities.base64Decode(dataURI.split(',')[1]);
else
byteString = decodeURI(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
return Utilities.newBlob(byteString, mimeString, "video.webm");
}
使用上面的答案发送表单。然后在应用程序脚本中发送表单,并使用上面的答案发送表单。然后在应用程序脚本中发送表单,其中提供给G+讨论的链接不再可用提供给G+讨论的链接不再可用