使用Google-Apps-Script将HTML表单转换为GoogleSheets

时间:2019-03-13 00:22:26

标签: javascript google-apps-script web-applications google-sheets html-form

使用google-apps-scripts将HTML表单数据发送到GoogleSheets文档时,我无法获得数据以进入正确的工作表。我确实将数据写入了Google工作表,因此可以确定我在正确更新gs应用程序时可以随时更新,但任何工作表都可以正常工作。我无法从第二个表单页面获取数据以转到单独的表格。

我是第二个学期的大学生,所以我不完全了解自己正在使用的所有内容,有些部分来自教程,另一些则来自我老师的投入(他很好奇我是否可以使用它)表格仅需10分钟,而引导模板仅用于进行基本测试。

我乐于接受所有建议以帮助解决此问题,但是如果它可以轻松扩展以能够在项目的后面合并更多选项卡,我将不胜感激。 感谢您的时间。

  

meetingeventtracker.html-表单部分fundrasingrequest.html是   基本上是相同的东西,但是ID和名称不同

            <div class="col-md-8">
                <input type="text" id="orgNameMET" name="orgNameMET" class="form-control-md form-control inputs" placeholder="Enter Organization Name">
                <input type="text" id="repNameMET" name="repNameMET" class="form-control-md form-control inputs" placeholder="Enter Name of Representative">
                <input type="email" id="repEmailMET" name="repEmailMET" class="form-control-md form-control inputs" placeholder="Enter Email of Representative">
                <input type="date" id="dateOfMeeting" name="dateOfMeeting" class="form-control-md form-control inputs" placeholder="Enter Date of Meeting/Event">
                <input type="time" id="timeOfEvent" name="timeOfEvent" class="form-control-md form-control inputs" placeholder="Enter Time of Meeting/Event">
                <input type="number" id="meetingCosts" name="meetingCosts" class="form-control-md form-control inputs" placeholder="Enter Cost of Meeting">
                <input type="text" id="futureEvents" name="futureEvents" class="form-control-md form-control inputs" placeholder="Enter Future Events">
                <input type="text" id="ifFund" name="ifFund" class=form-control-md form-control inputs="IF fundraiser" value="meet" style="opacity: 0;">
                <button type="submit">Submit Data</button>
            </div>
  

google-sheets.js我正在使用什么将数据从表单发送到   脚本

/*jslint browser: true*/
/*global $, jQuery, alert*/

// Variable to hold request
var request;
$("#foo").submit(function(event){
    if (request) {
        request.abort();
    }
    var $form = $(this);
    var $inputs = $form.find("input, button");
    var serializedData = $form.serialize();
    $inputs.prop("disabled", true);
    request = $.ajax({
        url: "https://script.google.com/macros/s/AKfycbzc9hr7HFK10LBVP_UnOvVyUsTDslD4GBq50olyGdP5br6TBkE/exec",
        type: "post",
        data: serializedData
    });
    request.done(function (response, textStatus, jqXHR){
        console.log("Hooray, it worked!");
        console.log(response);
        console.log(textStatus);
        console.log(jqXHR);
    });

    request.fail(function (jqXHR, textStatus, errorThrown){
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    request.always(function () {
        $inputs.prop("disabled", false);
    });
    event.preventDefault();
});
  

Code.gs是google apps脚本,我不知道要使用哪个参数来使应用程序根据提交的表单切换工作表中的标签

var tabs = [
    'MeetingEventTracker',
    'FundRaisingRequest'
];

//var SHEET_NAME = tabs[1];

var SCRIPT_PROP = PropertiesService.getScriptProperties();
function doGet(e) {
    return handleResponse(e);
}

function doPost(e) {
    return handleResponse(e);
}

function handleResponse(e) {
    var lock = LockService.getPublicLock();
    lock.waitLock(30000); // wait 30 seconds before conceding defeat.
    try {
      var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));

      for (i = 0; i < 12; i++ ){
        if (e.parameter[i] = "meet") {
            var SHEET_NAME = tabs[1];
        } else if (e.parameter[i] = "fund") {
            var SHEET_NAME = tabs[0];
        }
      }
        var sheet = doc.getSheetByName(SHEET_NAME);
        var headRow = e.parameter.header_row || 1;
        var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
        var nextRow = sheet.getLastRow() + 1; // get next row
        var row = [];
        // loop through the header columns
        for (i in headers) {

            if (headers[i] == "Timestamp") {
                row.push(new Date());
            } else {

                row.push(e.parameter[headers[i]]);
            }
        }
        sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
        return ContentService
            .createTextOutput(JSON.stringify({
                "result": "success",
                "row": nextRow,
            }))
            .setMimeType(ContentService.MimeType.JSON);
    } catch (e) {
        return ContentService
            .createTextOutput(JSON.stringify({
                "result": "error",
                "error": e,              
            }))
            .setMimeType(ContentService.MimeType.JSON);
    } finally { //release lock
        lock.releaseLock();
    }
}

function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}

0 个答案:

没有答案