I want eliminate formula information that is in an array of cells. My idea was to get the values of the cells as an array and then set the values back into the same place. I can't get it to work and have a feeling it due to my syntax. Any suggestions?
function getset() {
var ss = SpreadsheetApp.getActive();
var lr = sh.getLastRow();
var places = ss.getRange(2,11, lr-1).getValues();
ss.getRange(2,11, lr-1).setValues(places);
}
This works for me
function getset() {
var ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
var lr = sh.getLastRow();
var places = sh.getRange(2,11, lr-1).getValues();
sh.getRange(2,11, lr-1).setValues(places);
}
Related
So this might be a simple one for someone more skilled. The code works except it copies the functions rather than the values. The cells contain a LOT of functions and I'm using this to create a second version since the functions are UI data reliant and this is the easiest way to make a "Backup" in case we need to go back to an old forms data. I'm using the below code and it copies everything over, and creates the sheet with the right name, but I'm not sure how to get it to take the values and not the functions.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("AHA");
var nameSheet = sourceSheet.getRange(1, 15).getValue();
var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
var targetSheet = sourceSheet.copyTo(target);
targetSheet.setName(nameSheet);
I've tried this,
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("AHA");
var nameSheet = sourceSheet.getRange(1, 15).getValue();
var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
var targetSheet = sourceSheet.copyTo(target,SpreadsheetApp.CopyPasteType.PASTE_VALUES);
targetSheet.setName(nameSheet);
but it gives me the following error:
Exception: The parameters (SpreadsheetApp.Spreadsheet,SpreadsheetApp.CopyPasteType) don't match the method signature for SpreadsheetApp.Sheet.copyTo.
Any help or advice on another script I could use would be appreciated. Thanks.
Try this:
function copyvalues() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("AHA");
const vs = sh.getDataRange().getValues();
const name = sh.getRange(1, 15).getValue();
const tss = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
const tsh = tss.insertSheet(name);
tsh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}
I am a beginner with google script and I am facing a challenge with Google Sheets. The challenge is to write a function to reset a certain column's cells values to zeros. where I need it to rewrite any value was written in any cell to zero.
Like to reset the following:
To:
I tried this (Thanks to the gentlemen who answered):
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("scm");
var column = sh.getRange("d7:d14");
sh.getRange(1, column, sh.getLastRow(), 1).setvalue(0);
};
And the error appeared:
ERROR1
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("scm");
var column = sh.getRange("d7:d14");
const Z = 0
sh.getRange(1, column, sh.getLastRow(), 1),Z;
}
And the error appeared:
ERROR2
I need help with last line, which its purpose to rewrite the desired mentioned above column's values to zero.
Based on your example I recommend you to use an approach as follows.
function clearColumn() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var column = 2; // COLUMN B
sheet.getRange(1, column, sheet.getLastRow(), 1).setValue(0);
}
First you should select the sheet in SpreadsheetApp with a combination of SpreadsheetApp.getActive() and Sheet.getActiveSheet(). From that point you can select the column with Sheet.getRange() to change its value using Range.setValue(). Please let me know if you have any doubt about this procedure.
function resetColumn(col=1) {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('sheet name');
sh.getRange(1,col,sh.getLastRow(),1).clearContent();
}
regarding your update:
error1 issue:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("scm");
sh.getRange("d7:d14")setValue(0);//just from row 7 to row 14
//sh.getRange(1,4,sh.getLastRow()).setValue(0);//the whole column;
};
This version makes no sense to me:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("scm");
var column = sh.getRange("d7:d14");
const Z = 0
sh.getRange(1, column, sh.getLastRow(), 1),Z;
}
having trouble with the following code:
function addDelivery() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName("Sheet7");
var range = srcSheet.getRange("B3:L3").getValues();
var formulas = srcSheet.getRange("B3:L3").getFormulas();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,1,11).setValues(range);
range.clearContents();
range.setFormulas(formulas);
I want to clear the contents of cells B3:L3 but keep the formula in L3.
I get the error message "TypeError: range.clearContent is not a function".
I read that clearContent is a method of Range and clearContents is a method of sheet.
I've tried both, but neither work.
Please help.
Best regards
manc
Perhaps this would work better:
function addDelivery() {
var ss = SpreadsheetApp.getActive();
var srcSheet = ss.getSheetByName("Sheet7");
var range=srcSheet.getRange("B3:L3");
var values = range.getValues();
var formulas = range.getFormulas();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,1,11).setValues(values);
range.setFormulas(formulas);
srcSheet.getRange("B3:K3").clearContent();
I thought about it some more and I think I'd probably do it this way:
function addDelivery() {
var ss = SpreadsheetApp.getActive();
var srcSheet = ss.getSheetByName("Sheet7");
var range = srcSheet.getRange("B3:L3");
var values = range.getValues();
var formula = srcSheet.getRange("L3").getFormula();
srcSheet.insertRowBefore(6);
srcSheet.getRange(6,2,values.length,values[0].length).setValues(values);
srcSheet.getRange("L3").setFormula(formula);
srcSheet.getRange("B3:K3").clearContent();
How can I modify the below to give me the last row of column A. At the moment it calculates the last row of all the columns.
function Test() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
Browser.msgBox(lastRow);
}
I saw the below in another post and tried replacing mySheet with ss but that didn't give me the correct output.
var lastCell = mySheet.getRange(mySheet.getLastRow(),1).getNextDataCell(
SpreadsheetApp.Direction.UP);
Try this -
function Test() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var colAValues = ss.getRange("A:A").getValues().filter(function(el){
return el != "";
});
var lr = colAValues.length;
Browser.msgBox(lr); // replaced "lastRow" with "lr"
}
Hope this helps but do please let know in case it doesn't :)
I am looking for a little bit help on this situation. I have a spreadsheet where I create a new sheet, I rename it, and once this is done I do the same for like around 10 other spreadsheets, I would like to know how to implement a for or a while loop so that I dont have to type the repeated code 10 times. My code is below, any help would be really appreciate it. I have the keys in another document, so basically I need to know how to be able to read the keys in a variable and then be able to read each key since they are in a vertical column.
function create(){
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
ss0.setActiveSheet(ss0.getSheetByName("old"));
ss0.setActiveSheet(ss0.duplicateActiveSheet());
ss0.renameActiveSheet("main");
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
ss0.getRange("A4:I500").clearContent();
//repeated code key 1
var ss0 = SpreadsheetApp.getActiveSheet();
var master = SpreadsheetApp.openById("key1");
ss0.copyTo(master);
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
master.setActiveSheet(master.getSheetByName("Copy of main"));
master.renameActiveSheet("main");
// key 2
var ss0 = SpreadsheetApp.getActiveSheet();
var master = SpreadsheetApp.openById("key2");
ss0.copyTo(master);
var ss0 = SpreadsheetApp.getActiveSpreadsheet();
master.setActiveSheet(master.getSheetByName("Copy of main"));
master.renameActiveSheet("main");
}
You need to get the keys in an array and loop over this array.
Here is an outline
function myFunction() {
//get the keys in an array
//say, they are in column A in a spreadsheet from A1:A10
var keyArray = SpreadsheetApp.openById("ID of the ss with key").getSheetByName("name of the sheet with key")
.getRange("A1:A10").getValues();
var ss0 = SpreadsheetApp.getActiveSheet();
//loop for all the spreadsheets
for(var i in keyArray){
var ss = SpreadsheetApp.openById(keyArray[i][0]);
ss0.copyTo(ss);
ss.getSheetByName("Copy of main").setName("main")
}
}