Test1 Sheet
Test2 Sheet
I want to compare News01 from Test1 A Column with Test2 A Column, and need to fetch the corresponding Test2 B column value
So the result should be Finance in Sheet1 B column
But If I use else, even when if statement is true it's going to else statement.
If I delete else statement, then if statement is working.
I don't know why when if statement is true, it's going to else statement by default
function test() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = sheet.getSheetByName("Test1");
var target_sheet = sheet.getSheetByName("Test2");
var lastRow = source_sheet.getLastRow();
var inputs = source_sheet.getRange('A' + lastRow).getValues().flat();
var days = target_sheet.getRange('A1:A').getValues().flat();
var codes = target_sheet.getRange('B1:B').getValues().flat();
inputs.forEach(function(input, count){
for(var i = 0; i < days.length; i++){
if(days[i].trim() == input.trim()){
source_sheet.getRange('B' + (count + source_sheet.getLastRow())).setValue(codes[i]);
Logger.log(codes[i]);
break;
}
else{
Logger.log("News not found")
}
}
});
}
You are logging News not found for each comparisons with all the rows of Test2, which is improper.
Instead, you should only log when the comparison is finished.
function test() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = sheet.getSheetByName("Test1");
var target_sheet = sheet.getSheetByName("Test2");
var lastRow = source_sheet.getLastRow();
var inputs = source_sheet.getRange('A' + lastRow).getValues().flat();
var days = target_sheet.getRange('A1:A').getValues().flat();
var codes = target_sheet.getRange('B1:B').getValues().flat();
inputs.forEach(function(input, count){
let found = false;
for(var i = 0; i < days.length; i++){
if(days[i].trim() == input.trim()){
source_sheet.getRange('B' + (count + source_sheet.getLastRow())).setValue(codes[i]);
Logger.log(codes[i]);
found = true;
break;
}
}
if (!found) { Logger.log("News not found"); }
});
}
Or simply,
function test() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = sheet.getSheetByName("Test1");
var target_sheet = sheet.getSheetByName("Test2");
var lastRow = source_sheet.getLastRow();
var inputs = source_sheet.getRange('A' + lastRow).getValues().flat();
var days = target_sheet.getRange('A1:A').getValues().flat();
var codes = target_sheet.getRange('B1:B').getValues().flat();
inputs.forEach(function(input, count){
for(var i = 0; i < days.length; i++){
if(days[i].trim() == input.trim()){
source_sheet.getRange('B' + (count + source_sheet.getLastRow())).setValue(codes[i]);
Logger.log(codes[i]);
return;
}
}
Logger.log("News not found");
});
}
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh1 = ss.getSheetByName("Sheet0");
var sh2 = ss.getSheetByName("Sheet1");
var lastRow = sh1.getLastRow();
var vs1 = sh1.getRange(1,1,sh1.getLastRow()).getValues().flat().map(e => e.trim());
var vs2 = sh2.getRange(1,1,sh2.getLastRow(),2).getValues();
vs2.forEach(r => {
if(~vs1.indexOf(r[0].trim())) {
Logger.log(r[1]);
}
});
}
Related
I am using the script made by Mike Seekwell [Link] for connecting a MySQL database to a Sheet, and it works.
My problem is that I need it to run always from the first cell of the first sheet, while actually it can run from every active cell of every active sheet.
How can I modify this thing?
Here is the script:
/**
* #OnlyCurrentDoc
*/
var MAXROWS = 1000
var SEEKWELL_J_SHORT_DATES = { day: "yyyy-MM-dd", month: "yyyy-MM", year: "yyyy", dayNum: "dd", monthNum: "MM", yearNum: "yyyy", week: "W" }
var SEEKWELL_J_TIMEZONE = "UTC"
var HOST = '//host'
var PORT = '//port'
var USERNAME = '//username'
var PASSWORD = '//password'
var DATABASE = '/database'
var DB_TYPE = 'mysql'
function goToSheet(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName(sheetName));
};
function runSql(query, options) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getActiveSheet();
var sheetName = sheet.getName();
var cell = doc.getActiveSheet().getActiveCell();
var activeCellRow = cell.getRow();
var activeCellCol = cell.getColumn();
try {
var fullConnectionString = 'jdbc:' + DB_TYPE + '://' + HOST + ':' + PORT
var conn = Jdbc.getConnection(fullConnectionString, USERNAME, PASSWORD);
console.log('query :', query)
var stmt = conn.createStatement();
stmt.execute('USE ' + DATABASE);
var start = new Date();
var stmt = conn.createStatement();
stmt.setMaxRows(MAXROWS);
var rs = stmt.executeQuery(query);
} catch (e) {
console.log(e, e.lineNumber);
Browser.msgBox(e);
return false
}
var results = [];
cols = rs.getMetaData();
console.log("cols", cols)
var colNames = [];
var colTypes = {};
for (i = 1; i <= cols.getColumnCount(); i++) {
var colName = cols.getColumnLabel(i)
colTypes[colName] = { type: cols.getColumnTypeName(i), loc: i }
colNames.push(colName);
}
var rowCount = 1;
results.push(colNames);
while (rs.next()) {
curRow = rs.getMetaData();
rowData = [];
for (i = 1; i <= curRow.getColumnCount(); i++) {
rowData.push(rs.getString(i));
}
results.push(rowData);
rowCount++;
}
rs.close();
stmt.close();
conn.close();
console.log('results', results)
var colCount = results[0].length
var rowCount = results.length
var comment = "Updated on: " + (new Date()) + "\n" + "Query:\n" + query
if (options.omitColumnNames) {
results = results.slice(1)
rowCount -= 1
}
if (options.clearColumns && sheet.getLastRow() > 0) {
var startCellRange = sheet.getRange(startCell)
sheet.getRange(startCellRange.getRow(), startCellRange.getColumn(), sheet.getLastRow(), colCount).clearContent();
}
if (options.clearSheet) {
var startCellRange = sheet.getRange(startCell)
sheet.clear({ contentsOnly: true });
}
sheet.getRange(activeCellRow, activeCellCol, rowCount, colCount).clearContent();
sheet.getRange(activeCellRow, activeCellCol, rowCount, colCount).setValues(results);
var cell = sheet.getRange(activeCellRow, activeCellCol)
cell.clearNote()
cell.setNote(comment);
sheet.setActiveRange(sheet.getRange(activeCellRow + rowCount + 1, activeCellCol))
console.log('query success!, rows = ', rowCount - 1)
}
function runSqlFromSheet() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sql = doc.getRange('query!a2').getDisplayValue();
var options = {}
Logger.log('sql;', sql)
runSql(sql, options)
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('SeekWell Blog')
.addItem('Run SQL', 'runSqlFromSheet')
.addToUi();
}
function launch() {
var html = HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('SeekWell');
SpreadsheetApp.getUi()
.showSidebar(html);
}
I solved the problem. Not in the most elegant way, but it is a solution...
In order to run the script always on the same sheet, I substitute two variables.
The first variable to substitute is the following one:
var sheet = doc.getActiveSheet();
With this one (so it is always the first tab)
var sheet = doc.getSheets()[0];
Then the following one:
var cell = doc.getActiveSheet().getActiveCell();
With this one (so it is possible to define from which cell the script has to start, in this example from A1)
var cell = sheet.getRange('A1');
When I've tried just to setValue("random") to some cell with this trigger it does work, but when I've tried to execute this code(for returning certain sheet names from selected spreadsheet) it doesn't work! When I run that chunk of code as separate function it does work and return sheets normally.
So, why it doesn't execute part of code from "//RETURNING JUST BUILDING LIST" part when I change value in D2 cell?
function onEdit(e) {
var range = e.range;
var rangeEdit = e.range.getA1Notation();
if(rangeEdit == "D2"){
//RETURNING JUST BUILDING LIST
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheetsByName("OVERVIEW").getRange('A2').setValue("OK")
var sRNG = ss.getSheetByName("KEYS").getRange('A1:A12').getValues();
for (var z=0; z<sRNG.length; z++)
if (sRNG[z][0] == ss.getSheetByName("OVERVIEW").getRange('G3').getValue()) {
var xyz = z + 1;
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = ss.getSheetByName("OVERVIEW").getRange(1, 8, 50, 1);
range.clear();
var shArray = [];
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var spreadSheetsInA =
SpreadsheetApp.openByUrl(ss.getSheetByName("KEYS").getRange('B' + xyz).getValue()).getSheets();
shArray = spreadSheetsInA.map(function(sheet) {
return [sheet.getName()];
});
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = ss.getSheetByName("OVERVIEW").getRange(1, 8, shArray.length, 1);
range.setValues(shArray);
var rDEL = 1
for (var xdel = 0; xdel<shArray.length; xdel++){
if(shArray[xdel][0].indexOf("Overview")>-1 | shArray[xdel][0].indexOf("Cashflow")>-1 | shArray[xdel][0].indexOf("Dates")>-1){
var delSHT = ss.getSheetByName("OVERVIEW");
delSHT.getRange('H' + rDEL).clear();
}
rDEL++
}
}
}
Even when I've tried this, it doesn't work...
function onEdit(e) {
var range = e.range;
var rangeEdit = e.range.getA1Notation();
if(rangeEdit == "D2"){
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheetsByName("OVERVIEW").getRange("A2").setValue("ok")
}
}
Basically, I just need to execute code if value in D2 in sheet Overview is changed!
I need help to get a list of unique sorted value of a specific column (column B).
This is my try (It doesn't work), can you help me to find the error?
I'm not able to get the correct range in [object] format...
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function UniqueRange() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Prova')
var drng = sht.getDataRange();
var rng = sht.getRange(2,2, drng.getLastRow()-1,1);
var rngA = rng.getValues();
var shtout = s.getSheetByName('Nuovo');
var rngout = shtout.getRange(2,1,rngA.length,1)
var rngB = rngout.setValues(rngA);
var i = 0; i < rngA.length; i++
var rngB = rngB[i];
var unique = rngB.filter( onlyUnique );
}
Solution by Bellian:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function uniquevalues(){
var s = SpreadsheetApp.getActiveSpreadsheet();
var sht = s.getSheetByName('Prova')
var drng = sht.getDataRange();
var rng = sht.getRange(2,2, drng.getLastRow()-1,1); //range that contain all values
var rngA = rng.getValues();
var unique = [];
for(var i = 0; i < rngA.length; i++) {
var unique = unique.concat(rngA[i+1]);
}
unique = unique.filter(onlyUnique);
Logger.log(unique)
}
function getUniqueSortedArray(sheetname,column,datastartrow)
{
if(sheetname && column && datastartrow)
{
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName(sheetname);
var cntRg=cntSh.getDataRange().sort({column:column,ascending:true});
var vA=cntRg.getValues();
var uiA=[];
for(var i=datastartrow-1;i<vA.length;i++)
{
if(uiA.indexOf(vA[i][column-1])==-1)
{
uiA.push(vA[i][column-1]);
}
}
return uiA;
}
else
{
return 'Invalid Inputs';
}
}
And this is my modification of your code.
function UniqueSortedRange()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getSheetByName('Prova')
var rg=sh.getDataRange().sort({column:2,ascending:true});
var vA=rg.getValues();
var sh0=ss.getSheetByName('Nuovo');
var luneeko=[];
for(var i=1;i<vA.length;i++)
{
if(luneeko.indexOf(vA[i][1])==-1)
{
luneeko.push(vA[i][1]);
}
}
return luneeko;
}
function initializeData(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_view");
var selectedCell = SpreadsheetApp.getActiveRange();
var selectedMake = ss.getRange(2, 1).getValue();
var selectedModel = ss.getRange(2, 2).getValue();
var selectedYear = ss.getRange(2, 3).getValue();
var selectedTrim = ss.getarange(2, 4).getvalue();
var cars_raw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_raw");
// I think this is where I should be getting finale range or changing selected row to variable
var trims = [];
var carData = getCarData();
for (var i = 0; i < carData.length; i++) {
var row = carData[i];
var make = row[0];
var model = row[1];
var year = row[2];
var trim = row[3];
var data = row[$];// this is not a drop down
if (trim == "") {
continue;
}
if (make == selectedMake && model == selectedModel && year == selectedYear && trim == selectedTrim /* && data.indexOf(data)== -1*/) {
data.push(data);
}
}
// in case of no trims available
if (!trims.length) trims = ['N/A'];
}
function onEdit() {
Logger.log("onEdit()");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var selectedCell = SpreadsheetApp.getActiveRange();
var cars_raw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_raw");
Logger.log("Hello.");
if (sheet.getName() != "cars_view") {// changed sheet 7 to cars_view
return;
}
var selectedRow = selectedCell.getRow();
var selectedColumn = selectedCell.getColumn();
Logger.log("selected row=%s, column=%s", selectedRow, selectedColumn);
// Why does this below not start with 0 and go to 3;
if (selectedRow == 2.0) {
if (selectedColumn == 1.0) {
// make
initializeModels();
} else if (selectedColumn == 2.0) {
// model
initializeYears();
} else if (selectedColumn == 3.0) {
// year
initializeTrims();
} else if (selectedColumn == 4.0) {
initializeData();
}
}
You just need to copy values Ex:Hx from cars_raw sheet to A6:D6 of cars_view sheet.
For that, you can use setValues method of Range class
function initializeData(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_view");
var selectedCell = SpreadsheetApp.getActiveRange();
var selectedMake = ss.getRange(2, 1).getValue();
var selectedModel = ss.getRange(2, 2).getValue();
var selectedYear = ss.getRange(2, 3).getValue();
var selectedTrim = ss.getRange(2, 4).getValue();
var cars_raw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_raw");
var trims = [];
var carData = getCarData();
for (var i = 0; i < carData.length; i++) {
var row = carData[i];
var make = row[0];
var model = row[1];
var year = row[2];
var trim = row[3];
var data = row[4];
var battery_info = row.slice(4);
if (trim == "") {
continue;
}
if (make === selectedMake && model === selectedModel && year === selectedYear && trim === selectedTrim)
{
var range = ss.getRange("A6:D6");
range.setValues([battery_info]);
break;
}
}
}
I've changed the onEdit function as follows
function onEdit(e) {
var source = e.source;
var sheet_name = source.getSheetName();
var range = e.range;
var cars_raw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_raw");
if (sheet_name != "cars_view") {
return;
}
var selectedRow = range.getRow();
var selectedColumn = range.getColumn();
Logger.log("selected row=%s, column=%s", selectedRow, selectedColumn);
if (selectedColumn == 4 && selectedRow == 2) {
initializeData();
}
}
I also had to change getCarData function
function getCarData() {
var cars_raw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_raw");
return cars_raw.getSheetValues(3, 1, 5000, 8);
}
I'm getting HTML from a forum url, and parsing the post count of the user from their profile page. I don't know how to write the parsed number into the Google spreadsheet.
It should go account by account in column B till last row and update the column A with count.
The script doesn't give me any errors, but it doesn't set the retrieved value into the spreadsheet.
function msg(message){
Browser.msgBox(message);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Update")
.addItem('Update Table', 'updatePosts')
.addToUi();
}
function getPostCount(profileUrl){
var html = UrlFetchApp.fetch(profileUrl).getContentText();
var sliced = html.slice(0,html.search('Posts Per Day'));
sliced = sliced.slice(sliced.search('<dt>Total Posts</dt>'),sliced.length);
postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
return postCount;
}
function updatePosts(){
if(arguments[0]===false){
showAlert = false;
} else {
showAlert=true;
}
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var accountSheet = spreadSheet.getSheetByName("account-stats");
var statsLastCol = statsSheet.getLastColumn();
var accountCount = accountSheet.getLastRow();
var newValue = 0;
var oldValue = 0;
var totalNewPosts = 0;
for (var i=2; i<=accountCount; i++){
newValue = parseInt(getPostCount(accountSheet.getRange(i, 9).getValue()));
oldValue = parseInt(accountSheet.getRange(i, 7).getValue());
totalNewPosts = totalNewPosts + newValue - oldValue;
accountSheet.getRange(i, 7).setValue(newValue);
statsSheet.getRange(i,statsLastCol).setValue(newValue-todaysValue);
}
if(showAlert==false){
return 0;
}
msg(totalNewPosts+" new post found!");
}
function valinar(needle, haystack){
haystack = haystack[0];
for (var i in haystack){
if(haystack[i]==needle){
return true;
}
}
return false;
}
The is the first time I'm doing something like this and working from an example from other site.
I have one more question. In function getPostCount I send the function profileurl. Where do I declare that ?
Here is how you get the URL out of the spreadsheet:
function getPostCount(profileUrl){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var thisSheet = ss.getSheetByName("List1");
var getNumberOfRows = thisSheet.getLastRow();
var urlProfile = "";
var sliced = "";
var A_Column = "";
var arrayIndex = 0;
var rngA2Bx = thisSheet.getRange(2, 2, getNumberOfRows, 1).getValues();
for (var i = 2; i < getNumberOfRows + 1; i++) { //Start getting urls from row 2
//Logger.log('count i: ' + i);
arrayIndex = i-2;
urlProfile = rngA2Bx[arrayIndex][0];
//Logger.log('urlProfile: ' + urlProfile);
var html = UrlFetchApp.fetch(urlProfile).getContentText();
sliced = html.slice(0,html.search('Posts Per Day'));
var postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
sliced = sliced.slice(sliced.search('<dt>Total Posts</dt>'),sliced.length);
postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
Logger.log('postCount: ' + postCount);
A_Column = thisSheet.getRange(i, 1);
A_Column.setValue(postCount);
};
}
You're missing var in front of one of your variables:
postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
That won't work. Need to put var in front. var postCount = ....
In this function:
function updatePosts(){
if(arguments[0]===false){
showAlert = false;
} else {
showAlert=true;
}
There is no array named arguments anywhere in your code. Where is arguments defined and how is it getting any values put into it?