Javascript 通过Web应用程序上传图像后,将来自Google Drive的图像链接插入Google工作表
在前言中,我对编程非常陌生,对javascript一无所知 我正在开发一个web应用程序,用户可以通过点击按钮上传图片。此操作将使用唯一的文件夹和名称将图片上载到我的google drive中的某个目录中 现在,我正试图在上传图片的任何时候复制并粘贴图片的谷歌驱动链接 我能够在getFileUrl()方法中成功获取图片URL的ID。但是,当我在doStuff1()函数中调用该方法,然后将该信息插入userInfo.fileUrl时,我得到的是电子表格中的输出。 你怎么能称之为价值 更新:我意识到当我使用“google.script.run.getFileUrl(“fn”,“I”)时,也就是我得到“undefined”的时候。当我在本地运行getFileUrl()函数时,我确实得到了我想要的值。请告知我如何使用。正确使用SuccessHandler(函数),以便我可以返回“fileId0”的值。Javascript 通过Web应用程序上传图像后,将来自Google Drive的图像链接插入Google工作表,javascript,google-apps-script,web-applications,google-apps,Javascript,Google Apps Script,Web Applications,Google Apps,在前言中,我对编程非常陌生,对javascript一无所知 我正在开发一个web应用程序,用户可以通过点击按钮上传图片。此操作将使用唯一的文件夹和名称将图片上载到我的google drive中的某个目录中 现在,我正试图在上传图片的任何时候复制并粘贴图片的谷歌驱动链接 我能够在getFileUrl()方法中成功获取图片URL的ID。但是,当我在doStuff1()函数中调用该方法,然后将该信息插入userInfo.fileUrl时,我得到的是电子表格中的输出。 你怎么能称之为价值 更新:我意识到
这是前端,用户在这里上传图片 page.html
<html>
<head>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<div class="row">
<div class="file-field input-field">
<div class="waves-effect waves-light btn-small">
<i class="material-icons right">insert_photo</i>
<span>Import Picture</span>
<input id="files" type="file" name="image">
</div>
<div class="file-path-wrapper">
<input disabled selected type="text" class="file-path
validate" placeholder="Choose an image">
</div>
</div>
</div>
</form>
<?!= include("page-js"); ?>
</div> <!-- CLOSE CONTAINER-->
</body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script src="https://gumroad.com/js/gumroad.js"></script>
<script>
document.getElementById("addAnother").addEventListener("click",doStuff1);
var i=1;
var num={};
function doStuff1(){
num.picNum2=i;
var personName=document.getElementById("fn").value;
var fileId00=google.script.run.getFileUrl("fn","i");
var userInfo ={};
userInfo.firstName= document.getElementById("fn").value;
userInfo.number=i;
userInfo.fileUrl="https://docs.google.com/document/d/"+fileId00
+"/";
i++;
google.script.run.userClicked(userInfo);
}
var file,
reader = new FileReader();
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'- '+today.getDate();
reader.onloadend = function(e) {
if (e.target.error != null) {
showError("File " + file.name + " could not be read.");
return;
} else {
google.script.run
.withSuccessHandler(showSuccess)
.uploadFileToGoogleDrive(e.target.result,num.picNum,date,$('input#fn')
.val(),$('input#date').val());
}
};
function showSuccess(e) {
if (e === "OK") {
$('#forminner').hide();
$('#success').show();
} else {
showError(e);
}
}
function submitForm() {
var files = $('#files')[0].files;
if (files.length === 0) {
showError("Please select a image to upload");
return;
}
file = files[0];
if (file.size > 1024 * 1024 * 5) {
showError("The file size should be < 5 MB.");
return;
}
showMessage("Uploading file..");
reader.readAsDataURL(file);
}
function showError(e) {
$('#progress').addClass('red-text').html(e);
}
function showMessage(e) {
$('#progress').removeClass('red-text').html(e);
}
</script>
- 在“page js.html”的
中,您希望将从“code.gs”的doStuff1()
返回的值赋给getFileUrl()
userInfo.fileUrl
不返回值。当您想使用google.script.run.getFileUrl()
中的值时,您可以使用getFileUrl()
,如下修改的脚本所示withSuccessHandler()
doStuff1()
,如下所示
function doStuff1() {
num.picNum2 = i;
var personName = document.getElementById("fn").value;
google.script.run.withSuccessHandler(doStuff2).getFileUrl("fn","i"); // Modified
}
// Added
function doStuff2(fileId00) {
var userInfo = {};
userInfo.firstName = document.getElementById("fn").value;
userInfo.number = i;
userInfo.fileUrl = "https://docs.google.com/document/d/"+fileId00 +"/";
i++;
google.script.run.userClicked(userInfo);
}
注:
- 在这次修改中,我使用了
从doStuff2()
检索值。如果要修改函数名,请修改它getFileUrl()
- 在“page js.html”的
中,您希望将从“code.gs”的doStuff1()
返回的值赋给getFileUrl()
userInfo.fileUrl
不返回值。当您想使用google.script.run.getFileUrl()
中的值时,您可以使用getFileUrl()
,如下修改的脚本所示withSuccessHandler()
doStuff1()
,如下所示
function doStuff1() {
num.picNum2 = i;
var personName = document.getElementById("fn").value;
google.script.run.withSuccessHandler(doStuff2).getFileUrl("fn","i"); // Modified
}
// Added
function doStuff2(fileId00) {
var userInfo = {};
userInfo.firstName = document.getElementById("fn").value;
userInfo.number = i;
userInfo.fileUrl = "https://docs.google.com/document/d/"+fileId00 +"/";
i++;
google.script.run.userClicked(userInfo);
}
注:
- 在这次修改中,我使用了
从doStuff2()
检索值。如果要修改函数名,请修改它getFileUrl()
如果我误解了您的问题,并且这不是您想要的结果,我很抱歉。欢迎来到StackOverFlow,请借此机会学习如何和。欢迎来到StackOverFlow,请借此机会学习如何和。谢谢!这返回了一个链接!还有几个问题:正在添加一个包含用户条目的新行(请参见“Code.gs”->ws.appendRow(…)),一个链接被添加到数组“userInfo”中其他值的一个单独的行上。此外,当我传递用户输入“fn”和“I”时,我得到的是这个输出,而不是一个链接:“TypeError:无法调用未定义的方法”getFileByName。“。当我在getFileUrl(fn,I)函数中将fn和I设置为固定值时,这个问题就消失了,我得到了正确的输出(还手动创建了一个名为“fn”的文件夹和一个名为“I”的文件)。“你能帮忙吗?”哈里·李谢谢你的回答。我很高兴你的问题解决了。我愿意支持你。但你评论的问题是新问题,这与你的问题不同。那么,你能通过包含详细信息将其作为新问题发布吗?因为当你的初始问题被评论改变时,看到你的问题的其他用户会感到困惑。通过将其作为新问题发布,包括我在内的用户可以想到您的新问题。当时,。如果你能合作解决你的新问题,我很高兴。第一个问题——在另一行添加的链接——是一个自作自受的问题。请忽略此链接中的新问题:@Harry Lee谢谢您的回答。我问了你一个新问题。你能确认一下吗?谢谢!这将返回一个链接!还有两个问题:在添加包含用户条目的新行的过程中(请参见“Code.gs”->ws.appendRow(…),在数组“userInfo”中的其他值的单独行上添加了一个链接。此外,当我传递用户输入“fn”和“I”:“TypeError:无法调用未定义的”方法“GetFileByName”时,我得到的是这个输出,而不是一个链接。当我在getFileUrl(fn,I)函数中将fn和I设置为固定值时,这个问题就消失了,我得到了正确的输出(还手动创建了一个名为“fn”的文件夹和一个名为“I”的文件)。“你能帮忙吗?”哈里·李谢谢你的回答。我很高兴你的问题解决了。我愿意支持你。但你评论的问题是新问题,这与你的问题不同。那么,你能通过包含详细信息将其作为新问题发布吗?因为当你的初始问题被评论改变时,看到你的问题的其他用户会感到困惑。通过将其作为新问题发布,包括我在内的用户可以想到您的新问题。当时,。如果你能合作解决你的新问题,我很高兴。第一个问题——在另一行添加的链接——是一个自作自受的问题。请忽略此链接中的新问题:@Harry Lee谢谢您的回答。我问了你一个新问题。你能确认一下吗?