concatenate dynamic columns using google appscript - javascript

I want to concatenate columns that are dynamic, using appscript programmatically.
See the screenshot what my data looks like and what output i am trying to achieve:
I was able to achieve concatenation of static/fixed no. of columns using the below code.
function concatenate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getSheetByName("Sheet1");
var lr = sheetName.getLastRow();
var lc = sheetName.getLastColumn();
var rangeValue = sheetName.getRange(2, 1, lr-1, lc).getValues();
for(var i=0;i<rangeValue.length; i++) {
var setRange = sheetName.getRange(i+2, lc);
setRange.setValue(rangeValue[i][0] + " " + rangeValue[i][1] + " " + rangeValue[i][2])
};
};
All i want to achieve now is, the same above for DYNAMIC columns. I tried my best to figure it out, but in vain. Any help would be much greatly appreciated.

Splicing in another column
This assumes that you start with only your data columns and that you have not added the header for the new column. It can be made to work both ways but you just need to know what your starting with.
function myFunction() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet 1');
var rg=sh.getDataRange();
var vA=rg.getValues();
for(var i=0;i<vA.length;i++){
if(i==0){
vA[i].splice(vA[i].length,0,'Concatenate');
}else{
vA[i].splice(vA[i].length,0,vA[i].join(' '));
}
}
sh.getRange(1,1,vA.length,vA[0].length).setValues(vA);
}

I believe you can solve this by just looping through the columns using the last column index lc variable you already have. Should look something like this:
function concatenate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getSheetByName("Sheet1");
var lr = sheetName.getLastRow();
var lc = sheetName.getLastColumn();
var rangeValue = sheetName.getRange(2, 1, lr-1, lc).getValues();
for(var i = 0; i < rangeValue.length; i++) {
var catValues = "";
for (var j = 0; j < lc; j++) {
catValues += rangeValue[i][j] + " ";
}
var setRange = sheetName.getRange(i+2, lc);
setRange.setValue(catValues);
};
};
Or if you wanted to get fancy with .join():
function concatenate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getSheetByName("Sheet1");
var lr = sheetName.getLastRow();
var lc = sheetName.getLastColumn();
var rangeValue = sheetName.getRange(2, 1, lr-1, lc).getValues();
for(var i = 0; i < rangeValue.length; i++) {
var catValues = sheetName.getRange(i, lc).getValues().join(" ");
var setRange = sheetName.getRange(i+2, lc);
setRange.setValue(catValues);
};
};

Related

Update or Replace Row data in other sheet when unique id is found - Google Appscript

I need someone to modify this script that I found here to update or replace the row value of the sheet2 from sheet1 when a unique ID is found. This only works up to the 2nd column..I need something to update or replace rows in the sheet2 up to 10 columns or more. I have a sample spreadsheet. If you see on sheet2, some row values are missing or not yet updated, so I need those rows to be updated or replaced what's in sheet1.
Here is the spreadsheet:
https://docs.google.com/spreadsheets/d/100sjGCr0HSdJE1AERCJ9bGKiu3Vs2KxrvGIIjIrOjHw/edit?usp=sharing
Thanks for helping and sharing your knowledge.
function updateEntrees() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Sheet1');
var rg1a=sh1.getRange(2,1,sh1.getLastRow()-1,1);
var vA1a=rg1a.getValues();
var rg1b=sh1.getRange(2,2,sh1.getLastRow()-1,1);
var vA1b=rg1b.getValues();
var sh2=ss.getSheetByName('Sheet2');
var rg2a=sh2.getRange(2,1,sh2.getLastRow()-1,1);
var vA2a=rg2a.getValues();
var rg2b=sh2.getRange(2,2,sh2.getLastRow()-1,1);
var vA2b=rg2b.getValues();
for(var i=0;i<vA1a.length;i++) {
for(var j=0;j<vA2a.length;j++) {
if(vA1a[i][0]==vA2a[j][0]) {
vA2b[j][0]=vA1b[i][0]
}
}
}
rg2b.setValues(vA2b);
Try the following code, in the second line, change the value of "numColumns" as per your need:
function updateEntrees() {
var numColumns = 9;
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Sheet1');
var rg1a = sh1.getRange(2,1,sh1.getLastRow()-1,1);
var vA1a = rg1a.getValues();
var rg1b = sh1.getRange(2,2,sh1.getLastRow()-1,numColumns);
var vA1b = rg1b.getValues();
var sh2 = ss.getSheetByName('Sheet2');
var rg2a = sh2.getRange(2,1,sh2.getLastRow()-1,1);
var vA2a = rg2a.getValues();
var rg2b = sh2.getRange(2,2,sh2.getLastRow()-1,numColumns);
var vA2b = rg2b.getValues();
for( var i=0; i < vA1a.length; i++ ) {
for( var j=0; j < vA2a.length; j++ ) {
if( vA1a[i][0] == vA2a[j][0] ) {
vA2b[j] = vA1b[i];
}
}
}
rg2b.setValues(vA2b);
};

google script loop copy to loop row

Hi sorry this is probably a very newbie question, I am teaching myself and can't figure out how to google the answer to this one.
I have a loop in google sheets that goes through each field and updates some cells which pull info from a website that I then need to copy across into each row of the loop. It seems to be working through the loop okay however when trying to copy i cant get it into the correct row.
Im sure its something very simple hoping someone out there can point me in the right direction. I'm pretty sure its to do with the code at line 34 but can't figure out how to correct it.
function myLoop() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Lsheet = ss.setActiveSheet(ss.getSheetByName('Loop'), true);
var rangeData = Lsheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var dataRange = Lsheet.getRange(2,1, lastRow-1, lastColumn-1);
var data = dataRange.getValues();
var len = data.length;
var i = 0
for (i; i < len; i++) {
var row = data[i];
var player = row[4]
var prop = row[6]
var number = row[7]
var overodds = row[8]
var underodds = row[9]
var overreq = row[10]
var underreq = row[11];
if(prop = "Points"){
ss.setActiveSheet(ss.getSheetByName('Stats'), true);
ss.getRange('B1').activate();
ss.getRange("B1").setValue(player);
ss.getRange('AB5').activate();
ss.setActiveSheet(ss.getSheetByName('Loop'), true);
ss.getRange(overreq).activate();
ss.getRange('Stats!AB5').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
ss.setActiveSheet(ss.getSheetByName('Stats'), true);
ss.getRange('AB6').activate();
ss.setActiveSheet(ss.getSheetByName('Loop'), true);
ss.getRange(underreq).activate();
ss.getRange('Stats!AB6').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
}
}
Modifications of the original code:
Corrected an error in line 24. In your original post, you are using = (assignment operator) to compare your variables. However, you want to use == (equality operator).
Added missing semicolons. Although not necessary, makes for good, consistent style and may prevent some errors.
Moved i declaration inside the for-loop. Makes for cleaner, safer code.
Modified for-loop body so that it uses ranges and setValue(), getValue() methods to copy data.
function myLoop() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Lsheet = ss.setActiveSheet(ss.getSheetByName('Loop'), true);
var rangeData = Lsheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var dataRange = Lsheet.getRange(2,1, lastRow-1, lastColumn-1);
var data = dataRange.getValues();
var len = data.length;
var statsSheet = ss.getSheetByName('Stats');
var loopSheet = ss.getSheetByName('Loop');
for (var i = 0; i < len; i++) {
var row = data[i];
var player = row[4];
var prop = row[6];
var number = row[7];
var overodds = row[8];
var underodds = row[9];
var overreq = row[10];
var underreq = row[11];
if (prop == "Points") {
statsSheet.getRange('B1').setValue(player);
loopSheet.getRange(overreq).setValues(
statsSheet.getRange('AB5').getValues()
);
loopSheet.getRange(underreq).setValues(
statsSheet.getRange('AB6').getValues()
);
}
}
}
Could you try this:
var currentRow = ss.getActiveRange().getRow();
var currentCol = ss.getActiveRange().getColumn();
.
.
.
ss.getRange('State!AB5').copyTo(ss.getRange(currentRow+1, currentCol),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);

Setting a value in a drop down with Google Sheets using a script

I need to figure out a way to set a drop down value based on if another cell in the same row has a value. So for instance if D2 has a value other than '' or null; H2 needs to be set to "New Issue". The answers found in another similar post don't actually work with this since I am using formRedirector. Things got really weird last time. I tried to delete the earlier post but couldn't.
I have some of the logic figured out, but the issue is that what I have now writes "New issue" to H3 and below. Here is what I have so far:
var NEW_ISSUE = 'New Issue';
function defaultValue() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 900;
var dataRange = sheet.getRange(startRow, 4, numRows);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var default_status = row[7];
if (default_status != NEW_ISSUE && row == '') {
sheet.getRange(startRow + i, 8).setValue(NEW_ISSUE);
}
}
}
I have a feeling somewhere in the if statement I am messing up somewhere.
This solved the issue, thank you
var NEW_ISSUE ='New Issue';
var row;
var default_status;
function defaultValue() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1;
var numRows = 900;
var dataRange = sheet.getRange(startRow, 4, numRows);
var data = dataRange.getValues();
var statusRange = sheet.getRange(startRow,8,numRows);
var status = statusRange.getValues();
for (var i = 0; data[i] != ''; ++i)
{
if (status[i] == '')
{
sheet.getRange(i+startRow, 8).setValue(NEW_ISSUE);
}
}
}

How can add my javaScript total function with google spreadsheet, I'm not able select the range in one sheet where I need to get the result

I can add number in google spreadsheet with formula but I want to do it without formula, I can create total function with other editor and works fine but I cannot get it going with google spreadsheet.
function calculateSum(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheets()[0];
var cell=sheet.getRange("B22");
cell.setFormula("=sum(B17:B21)");
}
javaScript Code is Below:-
function addNumbers(a,b,c){
var sum=a+b+c;
return sum;
}
var result=addNumbers(2,3,4);
document.write(result);
I would do it like this:
function addRange() {
var myVals = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange("A1:A11").getValues();
var answer = 0;
for (var i = 0; i < myVals.length; i++) {
answer = answer + myVals[i][0];
}
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange("A12").getCell(1,1).setValue(answer);
}
To do multiple columns try this:
function addRange() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cellValues = sheet.getRange("A1:C12").getValues(); // 2D array
var columnCount = cellValues[0].length;
var rowCount = cellValues.length;
var answer = 0;
for (var row = 0; row < rowCount; row++) {
for (var column = 0; column < columnCount; column++) {
answer = answer + cellValues[row][column];
}
}
sheet.getRange("D12").getCell(1, 1).setValue(answer);
}

error in assigning values to google spreadsheet cell

I am new to google script. I want to assign a value to variable from spreadsheet cell and after doing arithmetic operations, want to assign it to particular cell in spreadsheet.
I am using following code :
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("PATTERNS");
var dd = sh.getRange("C4").getValue();
var ee = sh.getRange("D4").getValue();
var x = dd - ee;
sh.getRange("P4").setValue(x);
}
however, value at cell P4 is showing error : #NUM!
please help!
also i want to use for loop for cell no such as
for (var i = 1, i <60, i++)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("PATTERNS");
var dd = sh.getRange("C[i]").getValue();
var ee = sh.getRange("D[i]").getValue();
var x = dd - ee;
sh.getRange("P[i]").setValue(x);
}
How to get this? Can someone please help me?
Try this:
function myFunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getSheetByName("PATTERNS");
var rg=sh.getDataRange();
var vA=rg.getValues();
var change=false;
for(var i=0;i<vA.length;i++)
{
if(!isNan(vA[i][2]) && !isNan(vA[i][3]))
{
vA[i][15]=vA[i][2]-vA[i][3];
change=true;
}
}
if(change)
{
rg.setValues(vA);
}
}
Try this code
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("PATTERNS");
var dd = sh.getRange("C4").getValue();
var ee = sh.getRange("D4").getValue();
var x = parseInt(dd) - parseInt(ee);
sh.getRange("P4").setValue(x);
}

Categories

Resources