· 5 years ago · Nov 18, 2020, 03:56 PM
1const SPREADSHEET_ID = 'YOUR SPREADSHEET ID';
2const CLIENT_ID = 'YOUR CLIENT ID';
3const API_KEY = 'YOUR API KEY';
4const SCOPE = 'https://www.googleapis.com/auth/spreadsheets'
5const DISCOVERY_DOCS = ['https://sheets.googleapis.com/$discovery/rest?version=v4'];
6
7window.onload = function()
8{
9 // If you want to add onclick to wp's gutenburg button block, it needs to be done in the script.
10 $( "#submitBtn" ).click(function() {
11 get_data();
12 });
13
14
15}
16
17function get_data()
18{
19 var fName = $("#firstName").val();
20 var lName = $("#lastName").val();
21 var add1 = $("#address1").val();
22 var add2 = $("#address2").val();
23 var city = $("#city").val();
24 var country = $("#country").find(":selected").text();
25 var state_prov = $("#state-prov").val();
26
27 var data = [fName, lName, add1, add2, city, country, state_prov];
28
29 send_data(data);
30}
31
32function send_data(data)
33{
34 $.each(data, function(index, value)
35 {
36 console.log(value);
37 });
38
39 console.log("Sending Data!");
40
41 makeApiCall(data);
42}
43
44
45
46function makeApiCall(data) {
47
48var params = {
49 // The ID of the spreadsheet to update.
50 spreadsheetId: SPREADSHEET_ID,
51
52 // The A1 notation of a range to search for a logical table of data.
53 // Values will be appended after the last row of the table.
54 range: 'Sheet1',
55
56 // How the input data should be interpreted.
57 valueInputOption: 'RAW',
58
59 // How the input data should be inserted.
60 insertDataOption: 'INSERT_ROWS',
61};
62
63const valueRangeBody = {
64 'majorDimension': 'ROWS', //log each entry as a new row (vs column)
65 'values': data
66};
67
68var request = gapi.client.sheets.spreadsheets.values.append(params, valueRangeBody);
69 request.then(function(response) {
70 // TODO: Change code below to process the `response` object:
71 console.log(response.result);
72 }, function(reason) {
73 console.error('error: ' + reason.result.error.message);
74 });
75}
76
77function initClient() {
78
79 gapi.client.init({
80 'apiKey': API_KEY,
81 'clientId': CLIENT_ID,
82 'scope': SCOPE,
83 'discoveryDocs': DISCOVERY_DOCS,
84 });
85 //.then(function() {
86 //gapi.auth2.getAuthInstance().isSignedIn.listen(updateSignInStatus);
87 //updateSignInStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
88 //});
89}
90
91function handleClientLoad() {
92 gapi.load('client:auth2', initClient);
93}