<script>
$("#addopdfrm").submit(function(){
var opdname = $("#opdname").val();
var opdsex = $("#opdsex").val();
var opdage = $("#opdage").val();
var opdaddress = $("#opdaddress").val();
var opdcity = $("#opdcity").val();
var opdstate = $("#opdstate").val();
var opdpin = $("#opdpin").val();
var opdcon = $("#opdcon").val();
var opdmail = $("#opdmail").val();
var opdpresfee = $("#opdpresfee").val();
var odate = new Date();
var opdmonth = odate.getMonth()+1;
var opdyear = odate.getFullYear();
var opdday = odate.getDate();
var opddate = opdday+"/"+opdmonth+"/"+opdyear;
var opdtime = Date.now();
var mysql = require('promise-mysql');
var connection;
mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'plasmadoc'
}).then(function(conn){
connection = conn;
return connection.query("INSERT INTO `opdrec` VALUES('', '"+opdname+"', '"+opdage+"', '"+opdsex+"', '"+opdcon+"', '"+opdaddress+"', '"+opdpin+"', '"+opdcity+"', '"+opdstate+"', '"+opdmail+"', '"+opddate+"', 'no', '"+opdtime+"')");
}).then(function(rows){
var result = connection.query("INSERT INTO `opdvisits` VALUES('"+rows.insertId+"', '"+opddate+"', 'Prescription Fee', '"+opdpresfee+"', 'Waiting', 'no', '"+opdtime+"')");
connection.end();
return result;
}).then(function(rows){
// Logs out a ring that Frodo owns
alert("New Record Added");
});
});
</script>
I want to execute the first query. Retrieve the id of inserted row which is auto increment primary key and then use that id to insert data in different table.
Only the first query is executing. The second query is not executing. please help
Related
function myFunction() {
var ServerIP = 'xxx.xxx.xxx.xxx';
var SQL_Port = '3306';
var SQL_Usr = 'GDriveUser';
var SQL_Pwd = '**********';
var SQL_DB = 'wordpress_10';
var connectorInstance = 'jdbc:mysql://' + ServerIP+':'+SQL_Port;
var ConnectString = connectorInstance+'/'+SQL_DB;
var conn = Jdbc.getConnection(ConnectString, SQL_Usr, SQL_Pwd);
conn.setAutoCommit(false)
var SQLstatement = conn.createStatement();
var result = SQLstatement.executeQuery("SELECT * FROM `TABLE35` WHERE Team in ( 'atl', 'bkn', 'bos', 'cha', 'chi', 'cle', 'dal', 'den', 'det', 'gsw') and Date > 20201201 ORDER BY Date ASC");
var ss = SpreadsheetApp.openById("1QTv67oSTYnwsD0kwd0sDubzAor19pfAVngubjePqtzJg");
var sheet = ss.getSheetByName("Imported");
var cell = sheet.getRange('A2');
// loop through result object, setting cell values to database data
var row = 0;
while(result.next()) {
for(var i=0; i<23; i++) { // four fields per record
cell.offset(row, i).setValue(result.getString(i+1));
}
row++;
}
result.close();
SQLstatement.close();
conn.close();
}
This Google Script is only putting about 150 rows into my Google Sheet and then it times out.
Can someone help me find a fix for this?
Something like this:
function myFunction() {
var ServerIP = 'xxx.xxx.xxx.xxx';
var SQL_Port = '3306';
var SQL_Usr = 'GDriveUser';
var SQL_Pwd = '**********';
var SQL_DB = 'wordpress_10';
var connectorInstance = 'jdbc:mysql://' + ServerIP+':'+SQL_Port;
var ConnectString = connectorInstance+'/'+SQL_DB;
var conn = Jdbc.getConnection(ConnectString, SQL_Usr, SQL_Pwd);
conn.setAutoCommit(false)
var SQLstatement = conn.createStatement();
var result = SQLstatement.executeQuery("SELECT * FROM `TABLE35` WHERE Team in ( 'atl', 'bkn', 'bos', 'cha', 'chi', 'cle', 'dal', 'den', 'det', 'gsw') and Date > 20201201 ORDER BY Date ASC");
var ss = SpreadsheetApp.openById("1QTv67oSTYnwsD0kwd0sDubzAor19pfAVngubjePqtzJg");
var sheet = ss.getSheetByName("Imported");
var cell = sheet.getRange('A2');
// loop through result object, setting cell values to database data
var row = 0;
let oA=[];
sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).clearContent();//clear content before writing
while(result.next()) {
let rA=[];
for(var i=0; i<23; i++) {
rA.push(result.getString(i+1));
}
oA.push(rA);
}
sheet.getRange(2,1,oA.length,oA[0].length).setValues(oA);//starts at row 2 every time
result.close();
SQLstatement.close();
conn.close();
}
I have a google form setup to submit responses to a Google SQL Database upon submission. I can't seem to figure out how to get the responses off of the form and put them into the strings. Take a look:
var address = 'address';
var user = 'sysImporter';
var userPwd = 'password';
var db = 'db';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
function handleFormSubmit() {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse0 = formResponses[0];
var formResponse1 = formResponses[1];
var formResponse2 = formResponses[2];
var formResponse3 = formResponses[3];
var formResponse4 = formResponses[4];
var formResponse5 = formResponses[5];
var itemResponses0 = formResponse0.getItemResponses();
var itemResponses1 = formResponse1.getItemResponses();
var itemResponses2 = formResponse2.getItemResponses();
var itemResponses3 = formResponse3.getItemResponses();
var itemResponses4 = formResponse4.getItemResponses();
var itemResponses5 = formResponse5.getItemResponses();
var itemResponse0 = itemResponses0[0];
var itemResponse1 = itemResponses1[0];
var itemResponse2 = itemResponses2[0];
var itemResponse3 = itemResponses3[0];
var itemResponse4 = itemResponses4[0];
var itemResponse5 = itemResponses5[0];
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var stmt = conn.prepareStatement('INSERT INTO Volunteer '
+ '(FirstName, LastName, PrimaryPhone, SecondaryPhone, VolunteerEmail, Volunteerstatus) values (?, ?, ?, ?, ?, ? )');
stmt.setString(1, itemResponse1.getResponse());
stmt.setString(2, itemResponse2.getResponse());
stmt.setString(3, itemResponse3.getResponse());
stmt.setString(4, itemResponse4.getResponse());
stmt.setString(5, itemResponse0.getResponse());
stmt.setString(6, itemResponse5.getResponse());
stmt.execute();
}
The current error I get is:
TypeError: Cannot read property 'getItemResponses' of undefined
at handleFormSubmit(Code:24:38)
getResponse() retrieves all of the responses for a form, from there you must choose which response you want (in my case the most recent) and dissect it for the answers to the questions, here's what I ended up with:
function handleFormSubmit() {
//Grab form response list
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
//choose most recent response
var formLastResponse = formResponses.length - 1;
//grab items from response
var response = formResponses[formLastResponse];
var itemResponses = response.getItemResponses();
//set items to local variables
var email = response.getRespondentEmail();
var fname = itemResponses[0].getResponse();
var lname = itemResponses[1].getResponse();
var primaryphone = itemResponses[2].getResponse();
var secondaryphone = itemResponses[3].getResponse();
var staus = itemResponses[4].getResponse();
//connect to DB
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
//send SQL Statment to DB
var stmt = conn.prepareStatement('INSERT INTO Volunteer '
+ '(FirstName, LastName, PrimaryPhone, SecondaryPhone, VolunteerEmail, Volunteerstatus) values (?, ?, ?, ?, ?, ? )');
stmt.setString(1, fname);
stmt.setString(2, lname);
stmt.setString(3, primaryphone);
stmt.setString(4, secondaryphone);
stmt.setString(5, email);
stmt.setString(6, staus);
stmt.execute();
}
I’m trying to automate work routines with google forms, php and mysql. I did the following software architecture.
There is the form named as S, it is the static one, since I created it manually using GUI. Form Sform has the form submit trigger which puts data into the my mysql database.
After that data is processed by the php script which finally calls google script, which is published as a web application.
This script gets get parameters and parses them, gets new processed data from my database and programmatically builds forms depending on the selected data, finally it adds a trigger to dynamically (programmatically) created form or forms (let's name them as Dform).
The problem is that the function FormApp.getActiveForm() which is coded in the trigger of programmatically created form returns form id of the static form (which is manually created).
Here the code of static form Sform submit trigger:
function SaveData() {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var email = formResponse.getRespondentEmail();
var itemResponses = formResponse.getItemResponses();
var workDay = null;
var workFiles = null;
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
if (itemResponse.getItem().getTitle() == 'Pick the date of files you uploading') {
workDay = itemResponse.getResponse().toString();
}
if (itemResponse.getItem().getTitle() == 'Choose log files to be uploaded') {
workFilesIds = itemResponse.getResponse();
}
}
var dbUrl = '...';
var user = '...';
var userPwd = '...';
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var stmt = conn.prepareStatement('select worker_id from in_workers where worker_name = ?');
stmt.setString(1, email);
var results = stmt.executeQuery();
var worker_id = -1;
while (results.next()) {
worker_id = results.getString(1);
}
results.close();
if (worker_id == -1) {
stmt = conn.prepareStatement('INSERT INTO in_workers '
+ '(worker_name) values (?)', 1);
stmt.setString(1, email);
stmt.executeUpdate();
var res = stmt.getGeneratedKeys();
res.beforeFirst();
res.next();
worker_id = res.getInt(1);
}
var fName = '';
var fBlob = new Array(workFilesIds.length);
for (var j = 0; j < workFilesIds.length; j++) {
var file = DriveApp.getFileById(workFilesIds[j]);
fName = fName + '\n\r' + file.getName();
fBlob[j] = file;
stmt = conn.prepareStatement('INSERT INTO test_dep_files '
+ '(worker_id, file_name, file_date, file_body) values (?, ?, ?, ?)');
stmt.setString(1, worker_id);
stmt.setString(2, file.getName());
stmt.setDate(3, Jdbc.parseDate(workDay));
stmt.setBytes(4, file.getBlob().getBytes());
stmt.executeUpdate();
}
conn.close();
GmailApp.sendEmail('...', 'Control workplace form completed'
, 'User ' + email + ' uploaded data on ' + workDay
+ ' file list: ' + fName,
{attachments: fBlob, name: fName});
}
Here the code of script published as web application:
function doGet(e) {
var worker_id = e.parameter['worker_id'];
var form_trig_func_name = e.parameter['form_trig_func_name'];
var dbUrl = '...';
var user = '...';
var userPwd = '...';
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
stmt = conn.prepareStatement('select form_id from in_workers where worker_id = ?');
stmt.setString(1, worker_id);
var results = stmt.executeQuery();
var form_id;
results.beforeFirst();
while (results.next()) {
form_id = results.getString(1);
}
results.close();
var form;
try {
form = FormApp.openById(form_id);
var items = form.getItems();
var l = items.length;
for (var j = 0; j < l; j++) {
form.deleteItem(0);
}
} catch (e) {
form = FormApp.create('Enter additional products data');
}
stmt = conn.prepareStatement('select fc_id, fc_name_sys, fc_date from test_dep_fc t1'
+ ', test_dep_files t2 where t1.file_id = t2.file_id and fc_name_real is null '
+ 'and worker_id = ?');
stmt.setString(1, worker_id);
var results = stmt.executeQuery();
results.beforeFirst();
while (results.next()) {
form.addTextItem()
.setTitle(results.getString(2) + ' от ' + results.getString(3))
.setHelpText(results.getString(1));
}
results.close();
form_id = form.getId();
var allTriggers = ScriptApp.getUserTriggers(form);
var l = allTriggers.length;
for (var i = 0; i < l; i++)
ScriptApp.deleteTrigger(allTriggers[i]);
allTriggers = ScriptApp.getProjectTriggers();
l = allTriggers.length;
for (var i = 0; i < l; i++) {
if (allTriggers[i].getHandlerFunction() === 'saveData' + form_trig_func_name) {
ScriptApp.deleteTrigger(allTriggers[i]);
}
}
ScriptApp.newTrigger('saveData'+form_trig_func_name)
.forForm(form)
.onFormSubmit()
.create();
var form_url = form.getPublishedUrl();
stmt = conn.prepareStatement('update in_workers set form_id = ?, form_url = ? where worker_id = ?');
stmt.setString(1, form_id);
stmt.setString(2, form_url);
stmt.setString(3, worker_id);
stmt.execute();
conn.close();
return HtmlService.createHtmlOutput(form_id + '\n\r' + form_trig_func_name);
}
This is the code of dynamically (programmatically) created forms Dforms:
function saveData2() {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
var dbUrl = '...';
var user = '...';
var userPwd = '...';
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var fc_names_real = '';
var fc_ids = '';
var fc_names_sys = '';
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
var fc_id = itemResponse.getItem().getHelpText();
var fc_name_sys = itemResponse.getItem().getTitle();
var fc_name_real = itemResponse.getResponse().toString();
fc_names_real = fc_names_real + ' ' + fc_name_real;
fc_ids = fc_ids + ' ' + fc_id;
fc_names_sys = fc_names_sys + ' ' + fc_name_sys;
stmt = conn.prepareStatement('update test_dep_fc '
+ 'set fc_name_real = ? where '
+ 'fc_id = ?');
stmt.setString(1, fc_name_real);
stmt.setString(2, fc_id);
stmt.executeUpdate();
}
conn.close();
GmailApp.sendEmail('...', 'Control workplace form2 completed'
, 'Product serial nums: ' + fc_names_real
+ '\n\rProducts sys nums: ' + fc_names_sys
+ '\n\rProducts ids: ' + fc_ids
);
}
function saveData3() {
the same code as for saveData2
}
function saveData4() {
the same code as for saveData2
}
function saveData5() {
the same code as for saveData2
}
...
So the problem is that in saveData2 first code instruction returns wrong form id. It always returns form id of Sform.
Please help me with this, I'm ready to think that I have found a google forms bug.
Thanks a lot, Yaroslav
EDIT 1
hi #tehhowch, thank you, I did try, but, unfortunately, following code
function saveData2(e) {
//var form_id = e.source.getId();
...
returns wrong form id as well, it returns Sform id.
Finally this issue was solved next day it was written here. The solution is as simple as it is stupid one.
In the script published as web application code I add following instructions after form creation code. I save trigger function name to its properties.
PropertiesService.getDocumentProperties().setProperty(
'saveData'+form_trig_func_name, form_id);
Code to dynamically (programmatically) create forms Dforms was rewritten. When corresponding trigger fires I just take a formId in earlier saved property value.
function saveData2(e) {
//var form_id = e.source.getId();
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData2');
saveDataAll (form_id, 2);
}
function saveData3() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData3');
saveDataAll (form_id, 3);
}
function saveData4() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData4');
saveDataAll (form_id, 4);
}
function saveData5() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData5');
saveDataAll (form_id, 5);
}
function saveData6() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData6');
saveDataAll (form_id, 6);
}
function saveData7() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData7');
saveDataAll (form_id, 7);
}
function saveData8() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData8');
saveDataAll (form_id, 8);
}
function saveData9() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData9');
saveDataAll (form_id, 9);
}
function saveData10() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData10');
saveDataAll (form_id, 10);
}
function saveData11() {
var form_id = PropertiesService.getDocumentProperties().getProperty('saveData11');
saveDataAll (form_id, 11);
}
function saveDataAll (form_id, saveData)
{
var form = FormApp.openById(form_id);
...
I have not more then ten users of this functional and such workaround works for me. So the problem is closed.
I want to update a record. But every time i tried to update. It goes to catch statement. I am unable to find that where my query syntax is invalid.
It always goes to catch statement
I have no idea that either the query is wrong or any error is in the syntax.
if (IsPost) {
var value = Request.Form["value"];
var Student_Reg_No = Request.Form["Student_Reg_No"];
var Student_Name = Request.Form["Student_Name"];
var Father_Name = Request.Form["Father_Name"];
var Temporary_Address = Request.Form["Temporary_Address"];
var Permanent_Address = Request.Form["Permanent_Address"];
var Phone_No = Request.Form["Phone_No"];
var Blood_Group = Request.Form["Blood_Group"];
var Email_Address = Request.Form["Email_Address"];
if (Validation.IsValid()){
try{
var db = Database.Open("site_data");
var updateCommand = "Update site_data_table set Student_Reg_No =#0, Student_Name=#1, Father_Name=#2, Temporary_Address=#3,Permanent_Address=#4, Phone_No=#5, Blood_Group=#6, Email_Address=#7 where Id=#8)";
db.Execute(updateCommand, Student_Reg_No, Student_Name, Father_Name, Temporary_Address, Permanent_Address, Phone_No, Blood_Group, Email_Address, value);
Response.Redirect("~/edit?id=success");
}
catch{
<textarea>Data is Not updated</textarea>
}
}
}
var updateCommand = "Update site_data_table set Student_Reg_No =#0, Student_Name=#1, Father_Name=#2, Temporary_Address=#3,Permanent_Address=#4, Phone_No=#5, Blood_Group=#6, Email_Address=#7 where Id=#8)";
Change to
var updateCommand = "Update site_data_table set Student_Reg_No =#0, Student_Name=#1, Father_Name=#2, Temporary_Address=#3,Permanent_Address=#4, Phone_No=#5, Blood_Group=#6, Email_Address=#7 where Id=#8";
Im currently doing this loop on Node.js using the MySQL package
var room_array;
var room_array_list = [];
var room_list = connection.query('SELECT * FROM rooms ORDER BY id', function(err, result) {
room_array = result;
for(var i = 0; i < result.length; i++)
{
var room_name = result[i].room_name;
var room_desc = result[i].room_desc;
var room_creator;
var room_owner = connection.query('SELECT username FROM users WHERE id = ? LIMIT 1', result[i].user_id, function(error, user) {
room_array_list.push({'name': room_name, 'desc': room_desc, 'owner': user[0].username});
});
}
console.log(room_array_list);
But when I try to log the room_array_list Im just getting an empty array [].
So my question is how should I do this loop process to form an array with the room_name, room_desc and owner values?