I have written a code that generates random numbers and concatenates them with text. I am using this function to fill a range of cells (from A1 to Z10):
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1:Z10");
cell.setFormula('=CONCATENATE("61", RANDBETWEEN(1000000000, 9999999999), "#text")');
}
The problem with this is that every time I reload the page, the numbers are recalculated, there is no static value. I want the values in my range to become static, so that every time I reload the page, the numbers in between the "61" and "#text" remain the same. I have tried the following two codes, and neither has worked. The first did not receive an error message, but simply did not keep the values static, while the second received the error message ""
function copyFormulasToValues() { var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Sheet1");
var destinationSheet = ss.getSheetByName("Sheet1");
var range = sourceSheet.getRange(1,1,1,1);
range
.copyValuesToRange(destinationSheet, 1, 1, range.getNumRows(), range.getNumColumns());
};
The second code:
var studentSheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet();
// copyTo rows
sourceSheet.getRange(1, 1, 10, 26).copyTo(studentSheet.getRange(1, 1, 10, 26), {contentsOnly:
true});
SpreadsheetApp.flush(); // applies all values to the student spreadsheet
// and therefore all the formula cells update
// get value from calculated formula cell
studentSheet.getRange(1, 1, 10, 26).copyTo(sourceSheet.getRange(1, 1, 10, 26), {contentsOnly:
true});
}
The error message:
Cannot find method getRange(number,number,number,number). (line 11, file "Code")
I just want to copy and paste the values in the same spreadsheet so they remain static. How can I achieve this?
NOTE 1.
I really appreciate the help Tanaike. I tried out the script (which I have posted below), but when I try and run it, the function runs the code, and then just entirely deletes the values.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1:Z10");
cell.setFormula('=CONCATENATE("61", RANDBETWEEN(1000000000, 9999999999),
"#text.com")');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A1:Z10");
range.copyValuesToRange(sheet, 1, range.getNumColumns(), 1,
range.getNumRows());
}
Perhaps I am doing something wrong?
You want to remove the formulas and put the values to the cells of A1:Z10 which is the same range.
The formulas has already been put to the Spreadsheet.
If my understanding is correct, how about this answer?
Pattern 1:
In this pattern, copyValuesToRange is used.
Sample script:
function copyFormulasToValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A1:Z10");
range.copyValuesToRange(sheet, 1, range.getNumColumns(), 1, range.getNumRows());
}
I think that in your script, the arguments might be wrong. Please set sheet, column, columnEnd, row, rowEnd in order.
Pattern 2:
In this pattern, copyTo is used.
Sample script:
function copyFormulasToValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getSheetByName("Sheet1").getRange("A1:Z10");
range.copyTo(range, {contentsOnly: true});
}
In this case, please use copyTo of Class Range.
References:
copyValuesToRange(sheet, column, columnEnd, row, rowEnd)
copyTo(destination, options)
If I misunderstood your question and this was not the direction you want, I apologize.
Related
I have been using a script in my google sheet that I found here:
copy data from one sheet to another in google app script and append a row, one small issue
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,2,12,2);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
When I am running it now it gives me an error:
The coordinates of the target range are outside the dimensions of
the sheet.
which wasn't coming up before. Does anyone have an idea why it is doing this?
Issue:
The error is in this line:
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
pasteSheet.getLastRow() returns a row number which is at the very bottom of your sheet. Then you consider 12 rows range but the pasteSheet does not have 12 rows after the last row with content.
Solutions:
Add more rows in the pasteSheet:
or you can use insertRowsAfter:
pasteSheet.insertRowsAfter(pasteSheet.getLastRow(), 12)
Code snippet:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,2,12,2);
// insert rows to make sure you have enough space
pasteSheet.insertRowsAfter(pasteSheet.getLastRow(), 12)
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
Written a Google Sheet where when a button is pressed a collated row of data is sent to a different Google Sheet. It was launched six months ago among colleagues that have full permissions to both sheets. The reason for this post was that 1 in 10 submissions goes missing and never arrives to be added to the secondary Google Sheet. Thus wondering if anyone had any ideas as to if there was an issue with my code.
sh.getRange("j7").setValue('=now()');
var range = sh.getRange('J7:R7'); //Fixed range of where the row/data is collated
var data = range.getValues();
var tss = SpreadsheetApp.openById('SHEET ID');
var ts = tss.getSheetByName('Main');
ts.getRange(ts.getLastRow()+1,1,1,9).setValues(data);
Many thanks for any ideas...
Try this:
function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Enter Your Sheet Name");
sh.getRange("J7").setFormula('=now()');
var range = sh.getRange('J7:R7');
var data = range.getValues();
var tss = SpreadsheetApp.openById('SHEET ID');
var ts = tss.getSheetByName('Main');
ts.getRange(ts.getLastRow() + 1, 1, data.length, data[0].length).setValues(data);
}
I would like the below code to run on how ever many tabs get created - after Sheet 7 (First 7 tabs always remain unchanged). Currently I use an array and must number them which works if you know exactly how many tabs get created - which I dont always know. So I currently create script for [7] then [8] etc etc. this does return an error when I say have [20] but Tab 20 doesnt exist.
function Company_ONE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[7]; //SHEET 8
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheets()[7];
var lr = sheet.getLastRow();
var cell = SpreadsheetApp.getActive().getRange('AK3');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
sheet.getRange('AK3').activate();
var destinationRange = sheet.getActiveRange().offset(0, 0, lr-3);
sheet.getRange('AK3').copyTo(destinationRange);
}
Explanation:
Use getSheet().slice(7) to get from 8th sheet onwards. See here how slice works.
Then you can use forEach() to iterate through every sheet (after sheet 7th).
I also removed some unnecessary lines of codes. For example, you use SpreadsheetApp.getActive() multiple times in the sheet or you define the same variables twice like ss or sheet.
Since you are interacting with the sheets iteratively you might need to use SpreadsheetApp.flush() to make sure all the pending changes are completed.
Solution:
function Company_ONE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets().slice(7); // get 8th sheet onwards
sheets.forEach(sheet=>{
var lr = sheet.getLastRow();
var cell = ss.getRange('AK3');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
sheet.getRange('AK3').activate();
var destinationRange = sheet.getActiveRange().offset(0, 0, lr-3);
sheet.getRange('AK3').copyTo(destinationRange);
SpreadsheetApp.flush();
});
}
So I am working in GAS (Google Apps Script) and have taken a look at the following function:
function myFunction(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B5");
cell.setFormula("=SUM(B3:B4)");
}
I want to write a formula in GAS that concatenates two values with a Randbetween formula. When I click on a cell and type the code it gives the desired output:
=CONCATENATE("61", RANDBETWEEN(10, 99), "#text")
6177#text
But when I apply the principles in GAS, I receive error message.
Missing ) after argument list. (line 6, file "Code")
Here is the code I am using:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
cell.setFormula("=CONCATENATE("61", RANDBETWEEN(10, 99), "#text")");
}
Why won't this code work? How can I make it work?
Try this -
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
cell.setFormula('=CONCATENATE("61", RANDBETWEEN(10, 99), "#text")');
}
I replaced the double quotes and single quotes in the following line -
cell.setFormula('=CONCATENATE("61", RANDBETWEEN(10, 99), "#text")');
Hope this helps!
I want to select the range of 2 columns till the last row with a value. Now I use this code:
var test = declSheet.getRange('Potjes!A:B');
Form this sheet:
The result I get is this:
How can I select till the last row with value?
You can use Sheet.getDataRange function which:
Returns a Range corresponding to the dimensions in which data is
present.
It seems like you are only interested in column A and B so:
// var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// var sheet = spreadsheet.getActiveSheet();
var dataRange = sheet.getDataRange();
var desiredRange = sheet.getRange("A1:B" + dataRange.getLastRow());
// desiredRange will consist of A1:B5 in OP example