我有一个网页,我将使用任何人的收集文件并将其存储到我的谷歌驱动器(不是他们的),然后在电子表格中列出文件名和网址。为此,我选择使用gas作为我的页面和驱动器之间的接口,如下所示:
我的页面 - > GAS - >驱动
我的网页形式如下:
<form action="https://script.google.com/macros/s/some-random-string/exec"
method="post" enctype="multipart/form-data">
<input required type="file" name="thefile" id="thefile"><br />
<input type="submit" value="Upload File" name="submit">
</form>
&#13;
gs文件:
var spreadSheetId = 'string-of-my-spreadsheet';
function doPost(e) {
if(typeof e !== 'undefined'){
try {
var folderName = "myfolder";
var folder, folders = DriveApp.getFoldersByName(folderName);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(folderName);
}
var blob = e.parameter.thefile;
var fname = blob.getName();
var extensionfinder = /(?:\.([^.]+))?$/;
var ext = extensionfinder(fname)[1];
var sheet = SpreadsheetApp.openById(spreadSheetId).getActiveSheet();
var lastRow = sheet.getLastRow();
var firstCellInRowVal = sheet.getRange(lastRow, 1).getValue();
var newNameArr = firstCellInRowVal.split("-");
var newName;
if (newNameArr[0] == "File") {
newName = newNameArr[0]+"-"+(parseInt(newNameArr[1])+1);
}
else {
newName = 'File-1';
}
blob.setName(newName+'.'+ext);
var file = folder.createFile(blob)
var targetRange = sheet.getRange(lastRow+1, 1, 1, 2).setValues([[newName, file.getUrl()]]);
return ContentService.createTextOutput(newName);
}
catch (error) {
return ContentService.createTextOutput(error.toString());
}
}
}
&#13;
GAS返回此错误:
TypeError: Cannot call method "getName" of undefined.
任何人都有解决方案吗? thx之前..
答案 0 :(得分:0)
您的代码
var blob = e.parameter.thefile;
只是获取文件。要获得blob,您必须使用.getBlob()
,因此您的最终代码将是:
var blob = e.parameter.thefile.getBlob();
var fname = blob.getName();