HTML表单将数据发送到带有时间戳的谷歌表

时间:2018-02-28 02:54:43

标签: javascript html google-sheets google-sheets-api

我已成功将数据从HTML发送到我的Google工作表

function submit_form() {    
    var complete = true;
    var error_color = '#FFD9D9';
    var fields = ['name','phonenum','monthlysalary','totalamount','types'];
    var row = '';
    var i;
    for(i=0; i < fields.length; ++i) {
        var field = fields[i];
        $('#'+field).css('backgroundColor', 'inherit');
        var value = $('#'+field).val();       
        if(!value) {
            if(field != 'message') {
                $('#'+field).css('backgroundColor', error_color);
                var complete = false;
            }
            } else {            
            row += '"'+value+'",';
        }
    }
    if(complete) {      
        row = row.slice(0, -1);     
        var gs_sid = 'xxx';
        var gs_clid = 'xxx'; 
        var gs_clis = 'xxx'; 
        var gs_rtok = 'xxx'; 
        var gs_atok = false;
        var gs_url = 'https://sheets.googleapis.com/v4/spreadsheets/'+gs_sid+'/values/Google!A1:append?includeValuesInResponse=false&insertDataOption=INSERT_ROWS&responseDateTimeRenderOption=SERIAL_NUMBER&responseValueRenderOption=FORMATTED_VALUE&valueInputOption=USER_ENTERED';
        var gs_body = '{"majorDimension":"ROWS", "values":[['+row+']]}';        
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'https://www.googleapis.com/oauth2/v4/token?client_id='+gs_clid+'&client_secret='+gs_clis+'&refresh_token='+gs_rtok+'&grant_type=refresh_token');
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.onload = function() {            
            var response = JSON.parse(xhr.responseText);
            var gs_atok = response.access_token;            
            if(gs_atok) {
                var xxhr = new XMLHttpRequest();
                xxhr.open('POST', gs_url);
                xxhr.setRequestHeader('Content-type', 'application/json');
                xxhr.setRequestHeader('Authorization', 'OAuth ' + gs_atok );
                xxhr.send(gs_body);
            }            
        };
        xhr.send();
    }
}

现在我遇到了问题,我想让google工作表自动添加时间戳,当我将数据发送到谷歌表格时。无论如何要做到这一点?

谢谢

0 个答案:

没有答案