I am trying to use a textfinder to select a certain cell in a range. The textfinder returns null, despite the text that it's looking for is in the specified range. Anyone got a hand?
Cell E4 in sh1 (named: Check) is the variable that needs to be searched for in another range, because of this I am using var "trailer".
Range X4:X48 in sh2 (named: Lodge) has the value from E4 somewhere in that range, I am trying to select that specific cell.
The text in E4 changes based on criteria, that is why it needs to be variable instead of a set string.
Logger.log(trailer) returns the value of E4
Logger.log(test) returns null
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName("Check");
var sh2 = ss.getSheetByName("Lodge");
var range = sh1.getRange("E4");
var trailer = range.getValue();
Logger.log(trailer);
var test = sh2.getRange("X4:X48").createTextFinder(trailer).matchEntireCell(true).matchCase(false).matchFormulaText(true).getCurrentMatch();
Logger.log(test);
}
Try it this way:
function myfunk() {
const ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName("Sheet0");
var sh2 = ss.getSheetByName("Sheet1");
var range = sh1.getRange("A1");
var trailer = range.getValue();
var test = sh2.getRange("A1:X48").createTextFinder(trailer).matchEntireCell(true).matchCase(false).findAll();
test.forEach(rg => {
Logger.log('range: %s, value: %s',rg.getA1Notation(),rg.getValue());
})
}
Related
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
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])
});
}
Here is a script I'm using that works, but is trying to import too much data. I apologize for my ignorance when it comes to this.
function copyPaste() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName("Print Results");
var rangeSource = source.getDataRange();
var data = rangeSource.getValues();
var lr = rangeSource.getLastRow();
var lc = rangeSource.getLastColumn();
Logger.log(data);
var sss = SpreadsheetApp.openById("XXXXXX");
var target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,lr,lc).setValues(data);
}
What I need this to do is only select A8:M, and only copy over the rows that have a USER ID in column J. The USER ID is always a number, so a simple rule of ">0" should work for that. USER ID is imported from a different tab using a vlookup formula so the cell is not "empty".
Only copy rows where Column J is not null
function copyPaste() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Print Results");
const rg = sh.getDataRange();
const vs = rg.getValues().filter(r => r[9] != '');
Logger.log(vs);
const sss = SpreadsheetApp.openById("XXXXXX");
const target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,vs.length,vs[0].length).setValues(vs);
}
.filter() method
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;
}
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
}