0
私はGoogleドライブに接続し、ファイルを特定のフォルダにアップロードできるWebフォームを開発しました。現在のコードは最初に選択したファイルのみをアップロードするので、選択したすべてのファイルをアップロードできます。私の現在のコードは以下の通りです。助言がありますか?複数のファイルをGoogleドライブにアップロードできるようにGoogle Apps Scriptコードを変更するにはどうすればよいですか?
index.htmlを
<body>
<h2 class="col-sm-offset-4 col-sm-8" style="padding-bottom: 50px;">Smart-HR Connect Data Gather</h2>
<form class="form-horizontal" id="genericForm">
<div class="form-group" id="show-me6" style="display: none;">
<label class="col-sm-3 control-label">Please upload your Chart of Payroll Accounts & your Last Journal Entry:</label>
<div>
<input name="chartAccJournalEntry" type="file" id="file" multiple>
</div><br><br>
</div>
<input class="col-sm-offset-3" type="submit" onclick="this.value='Thank you ...'; google.script.run.withSuccessHandler(formSubmitted) .writeForm(this.parentNode); return false;">
</form>
<div id="output"></div>
<!-- where the confirmation message goes -->
<script>
var file,
reader = new FileReader();
// Upload the file to Google Drive
reader.onloadend = function(e) {
google.script.run
.withSuccessHandler(showMessage)
.uploadFileToGoogleDrive(
e.target.result, file.name,
$('input#distinguishedName').val()
);
};
function showMessage(e) {
$('#progress').html(e);
}
function formSubmitted(status) {
document.getElementById('genericForm').style.display = 'none'; //matches your form name or whatever you want to disappear post-submission
document.getElementById('output').innerHTML = status; //displays in item with the 'output' id
file = $('#file')[0].files[0];
reader.readAsDataURL(file);
showMessage("Uploading file..");
}
</script>
</body>
Code.gs
//basic function that builds the form using index.html as the template
function doGet(e) {
return HtmlService
.createTemplateFromFile('index')
.evaluate()
.setTitle("Smart-HR Connect Data Gather")
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function uploadFileToGoogleDrive(data, file, name, email) {
try {
var submissions = "Data Gather File Uploads";
var folder, folders = DriveApp.getFoldersByName(submissions);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(submissions);
}
var contentType = data.substring(5,data.indexOf(';')),
bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,')+7)),
blob = Utilities.newBlob(bytes, contentType, file);
folder.createFolder([name, email].join(" ")).createFile(blob);
return "OK";
} catch (f) {
return f.toString();
}
}