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;
}
Related
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);
}
So I understand this is very similar to:
Set Notes from Column A based on contents of Column B
function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var values_range = sheet.getRange("R5:R6");
var notes_range = sheet.getRange("AS5:AS6");
var notes = notes_range.getValues();
values_range.setNotes(notes)
}
However it's returning blank for me. It seems to be because the cells are the results of a SUM.
Is there a way to get the result?
Copy display values in column B as notes in column A
function addNote() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0")
const vs = sh.getRange(2,2,sh.getLastRow() - 1).getDisplayValues();
vs.forEach((r,i)=>{
sh.getRange(i + 2,1).setNote(r[0])
});
}
I am trying to get this script to work but cannot work it out.
So i would like the result on Sheet 1 from Sheet 4.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
ss.getRange('AM2').activate();
ss.getCurrentCell().setFormula('=[4] AL3');
I hope i am close ?
Explanation:
Firstly, define the sheets you want to work with:
var sheet1 = ss.getSheetByName('Sheet1');
var sheet4 = ss.getSheetByName('Sheet4');
Get the value of cell AL3 in Sheet4:
var value = sheet4.getRange('AL3').getValue();
Set it to cell AM2 in Sheet1:
sheet1.getRange('AM2').setValue(value);
If you want to use the formula instead, then just do that:
var sheet1 = ss.getSheetByName('Sheet1');
sheet1.getRange('AM2').setFormula('Sheet4!AL3');
Solution:
I think you are looking for this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet4 = ss.getSheetByName('Sheet4');
var value = sheet4.getRange('AL3').getValue();
// sheet1.getRange('AM2').setValue(value); // if you want the value use this
sheet1.getRange('AM2').setFormula('Sheet4!AL3'); // if you want the formula use this
}
If the name of the sheets have a space between the word and the number, for example Sheet 1 instead of Sheet1 and Sheet 4 instead of Sheet4, then use this solution:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet 1');
var sheet4 = ss.getSheetByName('Sheet 4');
var value = sheet4.getRange('AL3').getValue();
//sheet1.getRange('AM2').setValue(value); // if you want the value use this
sheet1.getRange('AM2').setFormula("'Sheet 4'!AL3") // if you want the formula use this
}
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 :)