So I have a sheet with about 800 rows. What I need to do is copy and paste each one 120 times. Instead of doing this manually I am hoping there is an automated way to do this in one shot. I have searched for a few things but everything I found had to do with copying the rows conditionally.
I don't need conditions. I just need all, each one of the 800 rows to be duplicated 120 times.
Any help or ideas is greatly appreciated.
Cheers
Modified #SpiderPig code to clone in a interleaved fashion
function duplicateRows() {
var sheet = SpreadsheetApp.getActiveSheet()
var numRows = sheet.getLastRow()
var numColumns = sheet.getLastColumn()
var numberOfClones = 120
for(var i = 0; i < numRows; i++) {
range = sheet.getRange((i*numberOfClones)+ 1, 1, 1, numColumns);
sheet.insertRows((i*numberOfClones)+ 2, numberOfClones-1)
range.copyTo(sheet.getRange((i*numberOfClones)+ 2 , 1, numberOfClones-1, numColumns));
}
}
Also instead of using a loop with range.copyTo(destination) to paste 120 times, you can expand the destination range to 120 rows. It will automatically paste the same value over the entire range.
So instead of this
for(var i = 1; i <= 120; i++) {
range.copyTo(sheet.getRange(numRows * i + 1, 1, numRows, numColumns));
}
you can do this once
range.copyTo(sheet.getRange(numRows * i + 1, 1, 120*numRows, numColumns));
Here is a script that will copy all the rows in the current sheet 120 times.
function duplicateRows() {
var sheet = SpreadsheetApp.getActiveSheet(),
numRows = sheet.getLastRow(),
numColumns = sheet.getLastColumn(),
range = sheet.getRange(1, 1, numRows, numColumns);
for(var i = 1; i <= 120; i++) {
range.copyTo(sheet.getRange(numRows * i + 1, 1, numRows, numColumns));
}
}
Related
Let's say I have a column of numbers:
1
2
3
4
5
6
7
8
Is there a formula that can calculate sum of numbers starting from n-th row and adding to the sum k numbers, for example start from 4th row and add 3 numbers down the row, i.e. PartialSum(4, 3) would be 4 + 5 + 6 = 15
BTW I can't use App Script as now it has some type of error Error code RESOURCE_EXHAUSTED. and in general I have had issue of stabile work with App Script before too.
As Tanaike mentioned, the error code when using Google Apps Script was just a temporary bug that seems to be solved at this moment.
Now, I can think of 2 possible solutions for this using custom functions:
Solution 1
If your data follows a specific numeric order one by one just like the example provided in the post, you may want to consider using the following code:
function PartialSum(n, k) {
let sum = n;
for(let i=1; i<k; i++)
{
sum = sum + n + i;
}
return sum;
}
Solution 2
If your data does not follow any particular order and you just want to sum a specific number of rows that follow the row you select, then you can use:
function PartialSum(n, k) {
let ss = SpreadsheetApp.getActiveSheet();
let r = ss.getRange(n, 1); // Set column 1 as default (change it as needed)
let sum = n;
for(let i=1; i<k; i++)
{
let val = ss.getRange(n + i, 1).getValue();
sum = sum + val;
}
return sum;
}
Result:
References:
Custom Functions in Google Sheets
Formula:
= SUM( OFFSEET( initialCellName, 0, 0, numberOfElementsInColumn, 0) )
Example add 7 elements starting from A5 cell:
= SUM( OFFSEET( A5, 0, 0, 7, 0) )
So I have this function here that runs through T2:T:
=IF($D$29<$N2,"", AVERAGE(INDIRECT("P"&IF($N2<11, 2,$N2-5)&":P"&$N2+5)))
Column P is a list of numbers starting at row 2. Column N is an index(goes up by 1 each row) which starts at row 2 and ends where P ends + 14, and D29 is just a number. In my current situation P ends at row 11 and N ends at row 25. And I'm trying to change it into an array formula so that when I add new rows it updates automatically. So after changing it I got this:
=ARRAYFORMULA(IF($D$29<$N2:N,"", AVERAGE(INDIRECT("P"&IF($N2:N<11, 2,$N2:N-5)&":P"&$N2:N+5))))
However, it is not functioning properly. It still occupies the same amount of rows, but each row is the same value. The value of the first row originally. How can I fix this problem? Thanks!
The problem here is that ARRAYFORMULA doesn't work with AVERAGE.
But you could always use javascript.
Open up the script editor and paste in this code.
function avg(nums, d) {
var r = [],
i, j, start, end, avg, count;
for(i = 0; i < nums.length; i++) {
if(d <= i) r.push([""]);
else {
if(i < 10) start = 0;
else start = i - 5;
end = i + 4;
avg = 0, count = 0;
for(j = start; j <= end; j++) {
if(nums[j]) {
avg += nums[j][0];
count++;
}
}
r.push([avg / count]);
}
}
return r;
}
Save it, go back to your spreadsheet and put this formula in any cell =avg(P2:P11, D29)
I have a spreadsheet where a lot of cells are multiplied with a CONSTANT value and added and the result (SOLL) differs from the expected (IST) value and the only way I was able to find, was to make 10 iterations modifying each time the CONSTANT value till the SOLL and IST are pretty equal.
My method looks too stupid and I am just asking a beautiful and intelligent way to solve it...
thanks
function costante()
{
var ss = SpreadsheetApp.getActiveSpreadsheet(); //spreadsheet "options"
SpreadsheetApp.setActiveSheet(ss.getSheets()[0]); //sheet nr.1
var sheet = SpreadsheetApp.getActiveSheet();
for(var i=0; i<10; i++){
var dataRange = sheet.getRange("o5:o5"); //marge ist
var mist = dataRange.getValues();
var dataRange = sheet.getRange("o4:o4"); //marge soll
var msoll = dataRange.getValues();
var dataRange = sheet.getRange("n1:n1"); //costante
var costante = dataRange.getValues();
var minus = 1;
if(mist - msoll < 0) { minus = -1};
costante = mist / msoll * minus * costante;
sheet.getRange("n1:n1").setValues([[costante]]);}
}
Maybe what you want, assuming your "lot of cells" are in A1:A10 and the IST value is in E1 is:
=E1/sum(A1:A10)
but please note the tag wiki.
thanks, I was first surprised by the simple method and I checked it:
10 values and their addition gives for instance = 10 (sum(a1:a10))
the IST value that I would like to have is 500 (E1)
E1/sum(A1:A10) = 500/10 = 50 = C (COSTANTE)
E1 = Sum(A1:A10) x C
if I multiply each A(y) value with 50, I have 500 and the proposed formula would perfectly work
BUT I forgot to mention that the values of A(y) are formulas that include already my COSTANTE it means that in reality each A(y) value is a formula that contains C
E1 = Sum( A(y) x C ) x C
a) for that reason the solution proposed does not give the needed value, and I was obliged to repeat the calculation ca.10 times
b) I also added a delay of 1 second because I was not sure if the spreadsheet is able to calculate so fast (please tell me if it is necessary)
c) and I said that if the result is +/- 2 from expected value, to stop to calculate
It works but is still not beautiful...
function costante()
{
var ss = SpreadsheetApp.getActiveSpreadsheet(); //spreadsheet "options"
SpreadsheetApp.setActiveSheet(ss.getSheets()[0]); //sheet nr.1
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("o5:o5"); //marge ist
var mist = dataRange.getValues();
for(var i=0; i<20; i++)
{
var dataRange = sheet.getRange("o4:o4"); //marge soll
var msoll = dataRange.getValues();
var dataRange = sheet.getRange("n1:n1"); //costante
var costante = dataRange.getValues();
var diff = msoll - mist;
if(diff < -2) { costante = costante * 1.005;}
else if (diff > 2) { costante = costante * 0.995;}
else {return;}
sheet.getRange("n1:n1").setValues([[costante]]);
Utilities.sleep(1000); //wait 1.5sec
}
}
I have a dynamic array. I need to display tableview as following scenario...
In the First cell i need to display 1 item.
In the second cell i need to display 2 items.
In the third cell i need to display 3 items.
In the forth cell i need to display 1 item.
In the fifth cell i need to display 2 items.
In the sixth cell i need to display 3 items.
and so on...
Could any one please suggest how to return no of rows in a section.
Try this :
int noOfRow = total/2 + ceil((total % 3)/3.0);
Simple logic for this is:
NoOfRows = TotalCount / 2
For e.g.:
If last value is 6 then, total no of rows are (6 / 2) = 3
If last value is 12 then, total no of rows are (12 / 2) = 6
You have to think logical that's it.
Hope this helps.
A faster method might be:
Notice in the divide by 2 method, most numbers work. The ones don't work are:
2, 4, 8, 10... basically, even numbers that aren't divisible by 6.
So we can come up with something like:
int count = array.count;
if (count % 2 == 0 && count % 6 != 0) {
count + 2;
}
int rows = ceilf(count / 2);
Or we can write a for loop:
int counter = array.size;
int rows = 0;
int dec = 1;
while (counter > 0) {
rows++;
counter - dec;
dec = dec % 3 + 1;
}
The for loop is of course, slower.
I want to copy some columns to another sheet. When i call the copyValuesToRange method twice, one is working, but the other is not.
function myFunction() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("f3y53y54y45h45jh454");
var target_sheet = target.getSheetByName("Sheet2");
var sheet = s.getSheetByName("Sheet1");
var sheet_last_row = sheet.getLastRow() + 1;
var source_range = sheet.getRange("B1:H"+sheet_last_row);
var source_range2 = sheet.getRange("K1:P"+sheet_last_row);
var sWidth=source_range.getWidth() + 1;
var sHeight=source_range.getHeight() + 1;
var sWidth2=source_range2.getWidth() + 1;
var sHeight2=source_range2.getHeight() + 1;
var last_row=target_sheet.getLastRow();
source_range.copyValuesToRange(target_sheet , 1, sWidth, last_row + 1, last_row + sHeight );
source_range2.copyValuesToRange(target_sheet , 8, sWidth2, last_row + 1, last_row + sHeight2 );
}
How can i fix this problem or is there another solution for copying cells the another sheet.
Shift the width of the second range from the starting point to (the end point of the first range+1) by sWidth2, as it should be the column number of the target range right border:
source_range2.copyValuesToRange(target_sheet , sWidth1+1, sWidth1+1+sWidth2, last_row + 1, last_row + sHeight2 );