I am trying to update four out of five columns in FusionTables using data from Google Spreadsheet. I have just started programming Apps-scripts. I need help with update query. Any suggestions or feedback is appreciated. Thank you in advance.
function sync() {
var tasks = FusionTables.Task.list(TABLE_ID);
// Only run if there are no outstanding deletions or schema changes.
if (tasks.totalItems === 0) {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
//i = 1 for 1st row with values in spreadsheet. 0 for column headers.
for (var i = 1; i < data.length; i++) {
var cName = data[i][0];
var ed_vs = data[i][1];
var ed_vs_ada = data[i][2];
var color = data[i][3];
//update ft
var updateQry = "update "+TABLE_ID+" set ED_VS = "+ed_vs+",ED_VS_ADA = "+ed_vs_ada+", COLOR = "+color+ "where COUNTY = "+cName;
//FusionTables.Query.sql(updateQry);
//is this even possible to execute an update query? help with syntax?
Logger.log(updateQry);
}
}
};
Here's working solution for someone with similar issue.
function sync() {
var tasks = FusionTables.Task.list(TABLE_ID);
// Only run if there are no outstanding deletions or schema changes.
if (tasks.totalItems === 0) {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
//i = 1 for 1st row with values in spreadsheet. 0 will give column headers.
for (var i = 1; i < data.length; i++) {
var cName = data[i][0];
var ed_vs = data[i][1];
var ed_vs_ada = data[i][2];
var color = data[i][3];
//parse rowid from output
var selQry = "select ROWID FROM "+TABLE_ID+" where COUNTY = '"+cName+"' ";
var cdata = FusionTables.Query.sql(selQry);
var c_rowid = cdata.rows[0][0]; //rowid
Logger.log(c_rowid);
var updateQry = "update "+TABLE_ID+" set ED_VS = '"+ed_vs+"',ED_VS_ADA = '"+ed_vs_ada+"', COLOR = '"+color+ "' where ROWID = '"+c_rowid+"' ";
FusionTables.Query.sql(updateQry);
Logger.log(updateQry);
}
}
};
Related
For the life of me I dont know what is wrong with this code it records the correct number but doesn't add the number correctly unless i type it in myself.
https://docs.google.com/spreadsheets/d/1kxIzE_HcPWd82LpGqCXNxg0yczhcnA6yOYRnB4GuAZo/edit?usp=sharing
var column = 0;
var HEADER_ROW_COUNT = 1;
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var worksheet = spreadsheet.getSheetByName("RawData");
var rows = worksheet.getDataRange().getNumRows();
var vals = worksheet.getSheetValues(1, 1, rows, 1);
var max = 0;
for (var row = HEADER_ROW_COUNT; row < vals.length; row++) {
var splitstring = vals[row][0].toString().split(" ");
var splitstring = vals[row][0].toString().split(" ");
var id = splitstring[1];
Logger.log(id)
if (max < id) {
Logger.log(id)
max = id+1;
Logger.log(max)
}
}
var LastRow = worksheet.getLastRow();
worksheet.getRange(LastRow+1,1).setValue("PO# " + max.toString());
Although I'm not sure whether I could correctly understand your goal, in your script, var id = splitstring[1]; retrieved by var splitstring = vals[row][0].toString().split(" "); is the string type. I thought that this might be the reason of your issue. When this is reflected to your script, it becomes as follows.
From:
var id = splitstring[1];
Logger.log(id)
if (max < id) {
To:
var id = Number(splitstring[1]);
Logger.log(id)
if (max <= id) {
Note:
In your script, for example, you might be also able to use var max = Math.max(...vals.map(([v]) => Number(v.split(" ").pop()))) + 1; instead of your for loop.
Reference:
split()
Tryu this:
function myfunk() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("RawData");
Logger.log(parseInt(sh.getRange(sh.getLastRow(), 1).getValue().split(' ')[1]) + 1);
}
I'm trying to create a script that will create new documents from a template-document. Replace placeholders in the documents with data from the sheet based on a keyword search in a specific column. And then change the row's value in the specific column so that the row will not process when the script runs again.
I think I've got it right with the first keyword search, and the loop through the rows. But the last part to get the data to 'merge' to the placeholders I can't figure out how to. I just get the value "object Object" and other values in the document.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var lastColumn = s.getLastColumn();
function createDocFromSheet() {
var headers = getUpsertHeaders(s);//function is defined outside of this function
var statusColNum = headers.indexOf('Status')+1;
var row = getRowsData(s); //The function is defined outside this function.
for (var i=0; i<row.length; i++) {
var jobStatus = '';
if (row[i]['Status'] === '') {
//New: write the status to the correct row and column - this will be moved to the end when I get the rest right
var jobStatus = "Created";
s.getRange(i+2, statusColNum).setValue(jobStatus);
//Find the template and make a copy. Activate the body of the new file.
var templateFile = DriveApp.getFileById('1lkfmqsJMjjPujHqDqKtcDmL-5GMIxpOWTyCOaK29d2A');
var copyFile = templateFile.makeCopy()
var copyId = copyFile.getId()
var copyDoc = DocumentApp.openById(copyId)
var copyBody = copyDoc.getActiveSection()
//Find the rows Values as an object.
var rows = s.getRange(i+2,1,1,lastColumn)
var rowsValues = rows.getValues();
Logger.log(rowsValues)
//Until here I think it's okay but the last part?
//HOW TO replace the text???
for (var columnIndex = 0; columnIndex < lastColumn; columnIndex++) {
var headerValue = headerRow[columnIndex]
var rowValues = s.getActiveRange(i,columnIndex).getValues()
var activeCell = rowsValues[columnIndex]
//var activeCell = formatCell(activeCell);
Logger.log(columnIndex);
copyBody.replaceText('<<' + headerValue + '>>', activeCell)
}
Template doc : Link
Template sheet: Link
You can use the following GAS code to accomplish your goals:
var DESTINATION_FOLDER_ID = 'YOUR_DESTINATION_FOLDER_ID';
var TEMPLATE_FILE_ID = 'YOUR_TEMPLATE_FILE_ID';
function fillTemplates() {
var sheet = SpreadsheetApp.getActiveSheet();
var templateFile = DriveApp.getFileById(TEMPLATE_FILE_ID);
var values = sheet.getDataRange().getDisplayValues();
var destinationFolder = DriveApp.getFolderById(DESTINATION_FOLDER_ID);
for (var i=1; i<values.length; i++) {
var rowElements = values[i].length;
var fileStatus = values[i][rowElements-1];
if (fileStatus == 'Created') continue;
var fileName = values[i][0];
var newFile = templateFile.makeCopy(fileName, destinationFolder);
var fileToEdit = DocumentApp.openById(newFile.getId());
for (var j=1; j<rowElements-1; j++) {
var header = values[0][j];
var docBody = fileToEdit.getBody();
var patternToFind = Utilities.formatString('<<%s>>', header);
docBody.replaceText(patternToFind, values[i][j]);
}
sheet.getRange(i+1, rowElements).setValue('Created');
}
}
You only have to replace the 1st and 2nd lines as appropriate. Please do consider as well that the code will assume that the first column is the file name, and the last one the status. You can insert as many columns as you wish in between.
After some coding I ended up with this code to process everything automatic.
Again thanks to #carlesgg97.
The only thing I simply can't figure out now is how to generate the emailbody from the template with dynamic placeholders like in the document. How to generate the var patternToFind - but for the emailbody?
I've tried a for(var.... like in the document but the output doesn't replace the placeholders.
var DESTINATION_FOLDER_ID = '1inwFQPmUu1ekGGSB5OnWLc_8Ac80igK0';
var TEMPLATE_FILE_ID = '1lkfmqsJMjjPujHqDqKtcDmL-5GMIxpOWTyCOaK29d2A';
function fillTemplates() {
//Sheet variables
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
var values = sheet.getDataRange().getDisplayValues();
//Header variables
var headers = sheet.getDataRange().getValues().shift();
var idIndex = headers.indexOf('ID');
var nameIndex = headers.indexOf('Name');
var emailIndex = headers.indexOf('Email');
var subjectIndex = headers.indexOf('Subject');
var statusIndex = headers.indexOf('Status');
var fileNameIndex = headers.indexOf('File Name');
var filerIndex = headers.indexOf('Filer');
var birthIndex = headers.indexOf('Date of birth');
//Logger.log(statusIndex)
//Document Templates ID
var templateFile = DriveApp.getFileById(TEMPLATE_FILE_ID);
//Destination
var destinationFolder = DriveApp.getFolderById(DESTINATION_FOLDER_ID);
var templateTextHtml = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Email').getRange('D11').getValue();
//Run through the variables
for (var i=1; i<values.length; i++) {
//If first column is empty then stop
var index0 = values[i][0];
if(index0 == "") continue;
var rowElements = values[i].length;
var fileStatus = values[i][statusIndex];
//If the row already processed then stop
if (fileStatus == "Created") continue;
//If the row is not processed continue
//Define the new filename by the relevant Column
var fileName = values[i][fileNameIndex];
var newFile = templateFile.makeCopy(fileName, destinationFolder);
var fileToEdit = DocumentApp.openById(newFile.getId());
//Replace placeholders in the new document
for (var j=1; j<rowElements-1; j++) {
var header = values[0][j];
var docBody = fileToEdit.getBody();
var patternToFind = Utilities.formatString('{{%s}}', header);
docBody.replaceText(patternToFind, values[i][j]);
}
//Create the PDF file
fileToEdit.saveAndClose();
var newPdf = DriveApp.createFile(fileToEdit.getAs('application/pdf'));
DriveApp.getFolderById(DESTINATION_FOLDER_ID).addFile(newPdf);
DriveApp.getRootFolder().removeFile(newPdf);
newFile.setTrashed(true);
var newPdfUrl = newPdf.getUrl();
//Create the emailbody
var textBodyHtml = templateTextHtml.replace("{{Name}}",values[i][nameIndex]).replace("{{Date of birth}}",values[i][birthIndex]);
var textBodyPlain = textBodyHtml.replace(/\<br>/mg,"");
//Will send email to email Column
var email = values[i][emailIndex];
var emailSubject = values[i][idIndex]+" - "+values[i][fileNameIndex]+" - "+values[i][nameIndex];
MailApp.sendEmail(email,emailSubject,textBodyPlain,
{
htmlBody: textBodyHtml+
"<p>Automatic generated email</p>",
attachments: [newPdf],
});
sheet.getRange(i+1, filerIndex+1).setValue(newPdfUrl);
sheet.getRange(i+1, statusIndex+1).setValue('Created');
}//Close for (var i=1...
}
I tried to retrieve a list of agents who are stored in a google sheet. The problem that I am facing right now is that I can get the list of agents in the script file. However, when I try to pass it to an html file, I keep getting null for the list. I absolutely has no idea how that can be.
Can someone give me an idea where I should look for?
My script:
var DB_URL = "";
var AGENT_DB = "";
var CREATED_ON_IDX = 0;
var NAME_IDX = 1;
var EMAIL_IDX = 2;
function agentService_getAgents() {
var ss = SpreadsheetApp.openByUrl(DB_URL);
var sheet = ss.getSheetByName(AGENT_DB);
var dataRange = sheet.getDataRange();
var agents = [];
var values = dataRange.getValues();
for (var i = 1; i < values.length; ++i) {
var row = values[i];
var name = row[NAME_IDX];
var email = row[EMAIL_IDX];
var createdOn = row[CREATED_ON_IDX];
var agent = new Agent(name, email, createdOn);
agents[i-1] = agent;
}
Logger.log(agents);
return agents;
}
Ajax call in Html
<script type="text/javascript">
function onSuccess(agents) {
var $table = $("table");
console.log(agents);
}
google.script.run.withSuccessHandler(onSuccess)
.agentService_getAgents();
</script>
So Logger.log(agents) gives me a list of agent; but console.log(agents) gives me null.
I'm attempting to pull data from a mySQL table and populate it into a gsheet. I've written the following script, but I'm given an error at line 50 "Cannot convert array to Object". I'm certain this is a 2d array. I'm sure I'm missing something obvious, though, as this is my very first script.
function readFromTable() {
var ss = SpreadsheetApp.getActive();
var sheetDetails = ss.getSheetByName('Details');
var sheetData = ss.getSheetByName('Data');
var host = sheetDetails.getRange("B1").getValue();
var databaseName = sheetDetails.getRange("B2").getValue();
var userName = sheetDetails.getRange("B3").getValue();
var password = sheetDetails.getRange("B4").getValue();
var port = sheetDetails.getRange("B5").getValue();
var tableName = sheetDetails.getRange("B6").getValue();
var url = 'jdbc:mysql://'+host+':'+port+'/'+databaseName;
var sql = 'SELECT * FROM ' + tableName;
try{
var connection = Jdbc.getConnection(url, userName, password);
var results = connection.createStatement().executeQuery(sql);
var metaData = results.getMetaData();
var columns = metaData.getColumnCount();
//Retrieve metaData to a 2D array
var values = [];
var value = [];
var element = '';
//Get table headers
for(i = 1; i <=columns; i ++){
element = metaData.getColumnLabel(i);
value.push(element);
}
values.push(element);
//Get table data row by row
while(results.next()){
value = [];
for(i = 1; i <= columns; i ++){
element = results.getString(i)
value.push(element);
}
values.push(value);
}
//Close connection
results.close();
//Write data to sheet Data
sheetData.clear();
sheetData.getRange(1, 1, values.length, value.length).setValues(values);
SpreadsheetApp.getActive().toast('Your data has been refreshed');
}catch(err){
SpreadsheetApp.getActive().toast(err.message);
}
}
I'm French and beginner in the app script.
My Problem:
I get data from a "var resultat = UrlFetchApp.fetch(url).getContentText();" that gives me an XML file
I want to add the result in my spreadsheet BUT before I would like to know if the row already exists in my spreadsheet.
I try to do this:
function GetMeteo() {
url = "http://www.yr.no/place/France/Alsace/Strasbourg/forecast_hour_by_hour.xml";
var resultat = UrlFetchApp.fetch(url).getContentText();
var root = XmlService.parse(resultat).getRootElement();
var entries = root.getChild('forecast').getChild('tabular').getChildren();
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var newData = new Array();
for (var i =0 ; i < entries.length; i++) {
var heure = entries[i].getAttribute('from').getValue();
var precipitation = entries[i].getChild('precipitation').getAttribute('value').getValue();
var windDirection = entries[i].getChild('windDirection').getAttribute('deg').getValue();
var windSpeed = entries[i].getChild('windSpeed').getAttribute('mps').getValue();
var temperature = entries[i].getChild('temperature').getAttribute('value').getValue();
var pressure = entries[i].getChild('pressure').getAttribute('value').getValue();
var duplicate = -1;
Données.activate();
var data = Données.getDataRange().getValues();
for (j in data) {
if (data[j][0] == heure) {
duplicate = j;
}
else {
duplicate = -1;
}
}
if (NumLigne == -1) {
data.push({heure,precipitation,windDirection,windSpeed,temperature,pressure});
}
else {
data[NumLigne].push({heure,precipitation,windDirection,windSpeed,temperature,pressure});
}
}
But the DataPush does not work.