I want to to add value of cell B1 onto the existing value of A1. I would like the script to be relative, and leave the A1 cell as a pure value. Bonus if cell B1 is cleared at the end of the script.
function myFunction() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var CurrentCell = cellRange.getValue ();
var RightCell = getvalue.offset(0, 1);
Logger.log(`selectedColumn: ${selectedColumn}`);
Logger.log(`selectedRow: ${selectedRow}`);
Logger.log(`selected cell vale: ${cellRange.getValue()}`);
Logger.log(`selected cell vale: ${cellRange.getValue(0, 1)}`);
cellRange.setValue(CurrentCell+RightCell);
It leaves with me with the value of A1 plus 'Range'. If A1 is 7 it returns (7Range).
Trying to do this in Google App Scripts
Add value of cell one column to the right to current cell.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r.getValue());
}
I get the same thing you do when I replace this with:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r);
}
And I'm kind of surprised it doesn't return an error. But yeah it returns the class name
Related
I am trying to write a script to copy a range of cells to cell B2. The range to copy from changes each time so want to highlight the top left cell and then the script takes it from there. What I have so far is:
function CopyToB2() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var range = SpreadsheetApp.getActiveSheet().getRange(selectedColumn,selectedRow,2,43);
range.setValues(range);
spreadsheet.getRange('B2').activate();
(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
but get the error
Exception: The parameters (SpreadsheetApp.Range) don't match the
method signature for SpreadsheetApp.Range.setValues
Explanation:
The main issue with your code is that you pass a range object in the setValues function and therefore you are getting this error.
A second issue is that you passed the parameters of the getRange(row, column, numRows, numColumns) function in the wrong order. It should be:
getRange(selectedRow,selectedColumn,43,2)
instead of getRange(selectedColumn,selectedRow,2,43)
A third issue is that you need to define the range that the data will be pasted to and that can't be just a single cell B2 in this case. What you need to do is to define B2 as the start of your pasting range and then set the values of your active range.
Solution:
function CopyToB2() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var dataRange = sheet.getRange(selectedRow,selectedColumn,43,2).getValues();
sheet.getRange(2,2,dataRange.length,dataRange[0].length).setValues(dataRange);
};
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
}
I have the following data:
[Source] Sheet 1 (Quote Generator):
Sheet 1, Cell A4: Dropdown menu to select a job reference
Sheet 1, Cells K4>Q4:= figures pulled from google maps & calculations dependant on A4 menu selection
[Destination] Sheet 2 (Quote Log):
Sheet 2, Column B: Quote References
Sheet 2, Columns V>AB: Travel data and calculations from sheet 1
I'm after a script that will copy the data in Sheet 1, Cells K4:Q4, and paste them in to Sheet 2, Columns V:AB on the same row as the selected job ref '100001' on Sheet 1, Cell A4, which will also be present in Sheet 2, Column B
Any help would be greatly appreciated
TIA
The data range you have specified is large than the range you want to put data in hence I have reduced the source range in the code. Try this.
function copyrow() {
var ss = SpreadsheetApp.getActive();
var source = SpreadsheetApp.openById('SheetID');
var dest = SpreadsheetApp.openById('SheetID');
var sourcerange = source.getRange('B4:H4');
var sourcerangevalue = sourcerange.getValues();
var checkval = source.getRange('A4').getValue();
var lastrow = dest.getLastRow();
var destrange = dest.getRange('B1:B');
var destrangevalue = destrange.getValues();
for (i = 1; i <= lastrow; i++) {
if (checkval == destrangevalue[i]) {
dest.getRange('A15').setValue(destrangevalue[i])
dest.getRange("V" + i + ":AB" + i).setValues(sourcerangevalue)
};
};
};
function myFunction() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get data from Sheet1
var s = ss.getSheetByName("Sheet1");
var cel = s.getRange("A4").getValue();
var data = s.getRange("K4:Q4").getValues();
// get to Sheet2 and get values from column 'B'
var s = ss.getSheetByName("Sheet2");
var col = s.getRange("B1:B").getValues();
// find the row number in columnn B that contain the value
var row = col.flat().findIndex( c => c == cel ) + 1;
// put the 'data' into the cells of this row
if (row) s.getRange("V" + row + ":AB" + row).setValues(data);
}
Updated version:
function onEdit(e) {
if (e.range.getA1Notation() !== "Q6") return;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Sheet1");
var cel = s.getRange("A4").getValue();
var data = s.getRange("K4:Q4").getValues();
var s = ss.getSheetByName("Sheet2");
var col = s.getRange("B1:B").getValues();
var row = col.findIndex(c => c[0] == cel) + 1;
if (!row) return;
s.getRange("V" + row + ":AB" + row).setValues(data);
var s = ss.getSheetByName("Sheet1");
ss.getRange("A1").clear();
ss.getRange("F5").clear();
}
I am trying to copy and paste values of cell J3 from Activity Report to the first empty cell in column L.
The cell to copy is fixed, but the cell to paste in is variable.
So far the code I am using has only been able to copy the values from J3 to the last row of column L instead of the first blank row from the top. Any attempt to manipulate the destination has been unsuccessful. I was able to have it paste at the top but it kept overwriting the same cell.
function copyData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sName = ss.getSheetName();
if (sName == 'Activity Report'){
var source = ss.getRange("Activity Report!J3");
var destSheet = ss.getSheetByName("Activity Report");
var lastRow = destSheet.getLastRow();
var destination = destSheet.getRange("L"+lastRow);
destination.activate();
source.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
}
Thanks in advance!
Copying data to first empty cell in Columnn L
Copy value from J3 to bottom of column L
function copyData() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Activity Report');
sh.getRange(sh.getLastRow()+1,12).setValue(sh.getRange(3,10).getValue());
}
Try this:
This will put it into the very first empty cell in column L which could be the lastRow()+1 if there are no blanks.
function runOne() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Activity Report');
var f=false;
var L=sh.getRange(1,12,sh.getLastRow()+1,1).getValues().map(function(r,i){if(!f && r[0].toString().length==0){f=true;return i+1;}else{return '';}}).filter(function(e){return e;});
sh.getRange(L,12).setValue(sh.getRange(3,10).getValue());
}
This uses reduce:
If nothing else it's a bit simpler.
function copyData() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Activity Report');
var f=false;
var L=sh.getRange(1,12,sh.getLastRow()+1,1).getValues().reduce(function(a,r,i){if(!f && r[0].toString().length==0){f=true;return a + i;}return a;},1);
sh.getRange(L,12).setValue(sh.getRange(3,10).getValue());
}
Array.map
Array.filter
Array.reduce
I'm trying to adjust this script in google sheets. The goal is to retrieve the text from a cell column B and add this as a comment to the cell in column A.
The script that is hardcoded for a specific cell (A2 + B2)
`function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Test');
var targetCell = sheet.getRange("A2");
var sourceCell = sheet.getRange("B2");
var noteText = sourceCell.getValue();
targetCell.setNote(noteText);
}`
How do I change this into a dynamic script that retrieves the text in every row in column B and adds it to the comment of every cell of column A?
For those who is looking for a final solution:
function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange("A2:B5");
range.clearNote();
for (var x=1;x<5;x++) {
range.getCell(x, 1).setNote(range.getCell(x, 2).getValue());
}
}