Home > database >  how to call GAS functions in local html page
how to call GAS functions in local html page

Time:10-14

EDITED: im sorry i edited the topic to provide the full code

this is HTML code, its meant to be in the index.html inside the google script, but i moved it to my local page so i can pass the email and username directly to it from my database but now i run into problems such as i cant call GAS functions inside the javascript because its in a local file, before i used to call the script using with both HTML and code.gs in google app, but then the user will have to manually type in the name and email, i dont want that i was trying to pass the name and email to the script and then have the script pass them to its html form but it got too complicated for my level , so i really just wanna be able to pass the username and email to the script so the user doesnt have to type them, in whichever way is possible

         <!DOCTYPE html>
            <html>
            <head>
            <base target="_top">
            </head>
            <body>
                <form id="uploaderForm"  action="https://script.google.com/macros/.../EXEC>
                    <label for="uploaderForm">Upload plusieur fichiers</label>
                    <div>
                        <input type="text" name="applicantName" id="applicantName"
                            placeholder="nom">
                    </div>
                    <div>
                        <input type="text" name="applicantEmail" id="applicantEmail"
                            placeholder="Email">
                    </div>
                    <div>
                        <input type="file" name="filesToUpload" id="filesToUpload" multiple>
                        <input type="button" value="Submit">
                </div>
            </form>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <div id="output"></div>
            <script>
                var rootFolderId = 'XXXXXX-XXX-XXXXXXXXXXXXXXXX';
                var numUploads = {};
                numUploads.done = 0;
                numUploads.total = 0;
                // Upload the files into a folder in drive
                // This is set to send them all to one folder (specificed in the .gs file)
                function uploadFiles() {
                    var allFiles = document.getElementById('filesToUpload').files;
                    var applicantName = document.getElementById('applicantName').value;
                    if (!applicantName) {
                        window.alert('entrer le nom!');
                    }
                    var applicantEmail = document.getElementById('applicantEmail').value;
                    if (!applicantEmail) {
                        window.alert('entrer e-mail!');
                    }
                    var folderName = applicantName   ' '   applicantEmail;
                    if (allFiles.length == 0) {
                        window.alert('Aucun fichier sélectionné!');
                    } else {
                    

    numUploads.total = allFiles.length;
                    google.script.run.withSuccessHandler(function(r) {
                        // send files after the folder is created...
                        for (var i = 0; i < allFiles.length; i  ) {
                            // Send each file at a time
                            uploadFile(allFiles[i], r.folderId);
                        }
                    }).createFolder(rootFolderId, folderName);
                }
            }
    
            function uploadFile(file, folderId) {
                var reader = new FileReader();
                reader.onload = function(e) {
                    var content = reader.result;
                    document.getElementById('output').innerHTML = 'Téléchargement de '
                              file.name   '...';
                    //window.alert('uploading '   file.name   '...');               
                    google.script.run.withSuccessHandler(onFileUploaded)
                            .uploadFile(content, file.name, folderId);
                }
                reader.readAsDataURL(file);
            }
    
            function onFileUploaded(r) {
                numUploads.done  ;
                document.getElementById('output').innerHTML = 'Fichiers téléchargé '
                          r.fileName   ' ('   numUploads.done   '/'
                          numUploads.total   ' fichiers).';
                if (numUploads.done == numUploads.total) {
                    document.getElementById('output').innerHTML = 'Tous les '
                              numUploads.total   ' fichiers sont téléchargés et envoyés';
                    numUploads.done = 0;
                }
            }
        </script>
    </body>
    </html>

GAS CODE

function doGet() {
    return HtmlService.createHtmlOutputFromFile('index').setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

function createFolder(parentFolderId, folderName) {
    try {
        var parentFolder = DriveApp.getFolderById('1TDB4ewzgrGI77a9ajVtap2aRn4sAdyNm');
        var folders = parentFolder.getFoldersByName(folderName);
        var folder;
        if (folders.hasNext()) {
            folder = folders.next();
        } else {
            folder = parentFolder.createFolder(folderName);
        }
        return {
            'folderId' : folder.getId()
        }
    } catch (e) {
        return {
            'error' : e.toString()
        }
    }
}

function doPost(base64Data, fileName, folderId) {
    try {
        var splitBase = base64Data.split(','), type = splitBase[0].split(';')[0]
                .replace('data:', '');
        var byteCharacters = Utilities.base64Decode(splitBase[1]);
        var ss = Utilities.newBlob(byteCharacters, type);
        ss.setName(fileName);

        var folder = DriveApp.getFolderById(folderId);
        var files = folder.getFilesByName(fileName);
        var file;
        while (files.hasNext()) {
            // delete existing files with the same name.
            file = files.next();
            folder.removeFile(file);
        }
        file = folder.createFile(ss);
        return {
            'folderId' : folderId,
            'fileName' : file.getName()
        };
    } catch (e) {
        return {
            'error' : e.toString()
        };
    }
  
}

so finally the whole purpose is to hide the username and email input, and recieve the email and name from the page im calling the google app from

CodePudding user response:

Since you're just posting the form data, you can name the function you want to call as enter image description here

  • Related