Google Spreadsheet - Convert multiple columns to one column - for-loop

I want to loop through a set of rows in a Google Spreadsheet that look like this:
XXX 123 234 234
YYY 789 098 765
ZZZ 76 123 345
End Result Needs to Be:
XXX: 123
XXX: 234
XXX: 234
YYY: 789
YYY: 098
etc.
My current code:
function loopshoplocations(){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getRange('A4:A8').getValues();
var i=0;
for(i = 0; i < 4; i++){
return ('Shop Location: ' + data[i][0]);
}}

Alternatively with a formula
=ArrayFormula( transpose(split(query(rept(left(A2:A, 3)&" ", 3),,50000), " "))&": "
&transpose(split(query(regexreplace(A2:A, "^(.+?)\s",""),,50000), " ")))
Also see this screenshot:

This code assumes that there is a header row on row one. The data gets appended at the end of the sheet. If you want something different, the code would need to be adjusted.
function loopshoplocations() {
var data,L,outerArray,innerArray,numberOfRows,sheet,startRow;
startRow = 2;
sheet = SpreadsheetApp.getActiveSheet();
numberOfRows = sheet.getLastRow();
data = sheet.getRange(startRow,1,numberOfRows-startRow+1,4).getValues();
L = data.length;
//Logger.log(data);
outerArray = [];
var i,j;
for(i = 0; i < L; i++){
for (j=1; j<4 ; j+=1) {//Begin count at one, not zero
innerArray = [];//Reset
innerArray.push(data[i][0]);
innerArray.push(data[i][j]);
//Logger.log(innerArray)
outerArray.push(innerArray);
};
};
sheet.getRange(numberOfRows+1,1,outerArray.length,outerArray[0].length).setValues(outerArray);
};

Related

Is there any solution for getting next row in Sheet7

function Copy_data(){
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ts = sss.getSheetByName('Sheet7');
var nextrow = ts.getLastRow()+1;
var ss = sss.getSheetByName('Sheet6');
var lastRow= ss.getLastRow();
for(var i = 2; i<lastRow;i++){
var Date = ss.getRange(i,1).getValues();
var Product = ss.getRange(i,2).getValues();
var URL = ss.getRange(i,7).getValues();
if(URL = ss.getRange(i,7).getRichTextValue().getLinkUrl()){
for (var j = 2; j<nextrow;j++){
ts.getRange(j,1).setValues(Date);
ts.getRange(j,2).setValues(Product);
}}}}
My i loop is incrementing but j is not getting next row. i is putting value in row 2 of the Sheet7. It is just replacing value in second row. I did not getting result in the next row.

Simple nested for loop problems

I am very new to programming and I am trying to create a short macro on google sheets. I want to copy and paste a list of cells a certain number of times based on variables see here. On the first column you have a list of locations and the second column the number of times they should be pasted into a 3rd column (column F). For example i would like to paste A4 fives times into column F4. Then A5 twice into F8 (F4 + 5 rows) and so one until the end of my list.
I came up with the below code but it currently copies each locations of my list 5 times on the same rows (F4-F8) then repeat the same process on the following 5 rows, and so on. I think the issue is with the order of the loops but i can't figure it out really.
Another problem i have is that It only copies each location 5 times (B1), but i am not sure how to make my variable numShift an array for (B4-B16) so that each locations is copied the correct number of times
Any help would be really appreciated !
function myFunction() {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet()
var activeSheet = ss.getActiveSheet()
var numShift = activeSheet.getRange(4,2).getValue()
for (var k=0;k<65;k=k+5){
for (var indexNumLocation=0;indexNumLocation<15;indexNumLocation++){
for (var indexNumShift=0;indexNumShift<numShift;indexNumShift++)
{var locationRange = activeSheet.getRange(4+indexNumLocation,1).getValue();
activeSheet.getRange(4+k+indexNumShift,6).setValue(locationRange);
}
}
}
}
Try this
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = sheet.getLastRow();
var data = [];
var i, j;
// get times count for each row from B4 to last row
var times = sheet.getRange(4, 2, lastRow - 4, 1).getValues();
// get the data that needs to be copied for each row from A4 to last row
var values = sheet.getRange(4, 1, lastRow - 4, 1).getValues();
// loop over each row
for (i = 4; i <= lastRow; i++) {
// run loop number of times for that row
for (j = 1; j <= times[i]; j++) {
// push the value that needs to be copied in a temporary array
data.push([values[i][0]]);
}
}
// finally put that array data in sheet starting from F4
sheet.getRange(4, 6, data.length, 1).setValues(data);
}

Grouping with Knedo datasource / grid

How do I accomplish this in Kendo Grid/Datasource?
I have data that looks something like this:
Month | Value
--------------
1 | 10
1 | 15
1 | 30
2 | 5
2 | 7
3 | 4
I would like to group that data by month and output it to a grid in this form:
Month | Sum(Value)
------------------
1 | 55
2 | 12
3 | 4
I don't want any group headers and collapsable groups with items on it, I just want that simple output.
How can I do that in Kendo?
I don't think out of the box kendo could get you what you are wanting. You could manually massage the data into the structure you want, and then bind that to a kendo grid.
Take your array of data, group by month, then sum up the value(s);
var g = {};
var data = myData; // Or however you get your data
for(var i = 0; i < data.length; i++) {
var obj = data[i];
if(g[obj.month] === undefined) {
g[obj.month] = obj.value;
}
else {
g[obj.month] += obj.value;
}
}
var results = [];
for(var j in g) {
if(g.hasOwnProperty(j)) {
results.push({ month: j, value: g[j]});
}
}
See sample http://jsbin.com/pipinodoca/1/edit?js,output

How do I remove entries in a tuple with duplicated properties?

I have a tuple dictionary consisting of three attributes: Name, Address, Phone.
Example:
Johnny Tarr, 1234 Gaelic Way, 555-402-9687
Patrick Murphy, 1234 Taylor Road, 555-555-5555
Patrick Murphy, 1234 Morrison Court, 555-555-5555
How do I remove the entries where two of the three properties are duplicated?
A failed attempt to iterate the collection:
for (int i = 0; i < fileList.Count - 1; i++)
{
for (int j = i + 1; j < fileList.Count; j++)
{
// Test Results: There are supposed to be 362 duplicates. Outputting only 225 entries. A mix of duplicates and not duplicates.
if (fileList[i].Item1.Equals(fileList[j].Item1, StringComparison.CurrentCultureIgnoreCase) && fileList[i].Item3.Equals(fileList[j].Item3, StringComparison.CurrentCultureIgnoreCase))
{
file.WriteLine(fileList[i].Item1 + "|" + fileList[i].Item2 + "|" + fileList[i].Item3);
}
}
}
var distincts = fileList.GroupBy(t => t.Item1 + "," + t.Item3)
.Where(g => g.Count() == 1)
.Select(g => g.Single());
foreach (var item in distincts)
{
Console.WriteLine(item);
}
This groups your tuples by Name/Phone, then keeps only groups that contain single tuples, and then selects that single tuple for the output list of distinct tuples.

Formula to Split a Cell Part of a Row and Keep the Rest of the Rows (For a large dataset)

I have a large data set with 500+ rows and three columns: names, id, value. I want to split the names column into separate rows with one name each, but with copying over the same values for id and value.
For example, I have this:
names id value
bob, sam, bill 99 10
harris 95 11
george, john 98 14
tom, harry 97 13
And I need to get this:
bob 99 10
sam 99 10
bill 99 10
harris 95 11
george 98 14
john 98 14
tom 97 13
harry 97 13
I also need to be able to do this in one iteration or two, since I have a total of 500+ rows. I setup a test sheet here. Also I need the solution to be in Google Sheets.
I'm not familiar with the Spreadsheet API (and I'm loth to sign up to Google), but I think you should be able to do something like this:
var sheet = SpreadsheetApp.getActiveSheet();
var row = 2;
var col = 0;
var values = [];
var start = row;
for (;;) {
var range = sheet.getRange(row, col, 1, 3);
var data = range.getValues();
if (data[0] == "") break;
var names = data[0].split(/, */);
for (var i = 0; i < names.length; i++) {
values.push([names[i], data[1], data[2]]);
}
row++;
}
var range = sheet.getRange(start, col, values.length, 3);
range.setValues(values);
That expands your list in place, overwriting the original. You can make a copy by changing the rows and columns in the last range. (Anything not to do directly with the Spreadsheet API works. Everything else should work, but I haven't tested it.)

Resources