Try: Get row values of a spreadsheet. from specified columns
function getAllData() {
var url = 'sheetID';
var ss= SpreadsheetApp.openByUrl(url);
var sheet = ss.getSheetByName("Data");
//var range = sheet.getDataRange() //*This gives all data*
I tried by adding following.
var rangeList = sheet.getRangeList(['A1:B', 'E1:F']);
var values = rangeList.getRanges()
Logger.log(values)
return values
}
But log execution says "Info [Range, Range]" .
But I need to display all values of column A, B, E & F in each row.
Please guide me on this.
Just map the one you want:
function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2,1,sh.getLastRow() - 1, sh.getLastColumn()).getValues();
const o = vs.map(([a,b,,,e,f]) => [a,b,e,f]);
Logger.log(JSON.stringify(o));
const osh = ss.getSheetByName("Sheet1");
osh.clearContents();
o.unshift(["Product Name","Price1","Price4","UserID"])
osh.getRange(1,1,o.length,o[0].length).setValues(o);
}
Related
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'm having a problem with finding conditional values and replacing them in a Google sheet data array.
I have an array of data in column A.
I want to find the values without the "." is an integer
And add a . and any natural number.
Write new data in column B.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName('Sheet1')
var range = sheet.getRange('a:a')
}
I am new with GAS, and this topic will help me so much,
Thank you for give me a help.
function myFunction() {
const n = 123;
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Sheet1');
const row = sheet.getLastRow();
const range = sheet.getRange(1, 1, row, 1);
const values = range.getValues();
const results = values.map(e => {
if (Number.isInteger(e[0])) {
return [`${e[0]}.${n}`];
}
else {
return [''];
}
});
sheet.getRange(1, 2, row, 1).setValues(results);
}
I want to copy the cell values from b1 to b2 but I'm getting error please help how to fix this
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = ss.getSheetByName("b1");
var source = inputSheet.getRange('B3','D3','F3','H3','J3','L3','N3','P3','R3','T3','V3');
var values =
source.getValues();
var outputSheet = ss.getSheetByName("b2");
outputSheet.getRange(outputSheet.getLastRow()+1,1,values.length,values[0].length).setValues(values);
};
Exception: The parameters (String,String,String,String,String,String,String,String,String,String,String) don't match the method signature for SpreadsheetApp.Sheet.getRange.
recordHistory # RecordPercent.gs:6
You should consider using Sheet.getRangeList()
Sheet.getRange() method has four methods which accept upto four parameters and they are all explained on this page
function getAndSetValueWithRangeList() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('b1');
const osh = ss.getSheetByName('b2');
let rlA = sh.getRangeList(['B3', 'D3', 'F3', 'H3', 'J3', 'L3', 'N3', 'P3', 'R3', 'T3', 'V3']);
rlA.getRanges().forEach((r,i)=>{osh.getRange(r.getA1Notation()).setValue(r.getValue());});
}
I have a database that makes live calculations, but once the new data added to the database has been processed that day, I effectively want to Copy + Paste value only on these cells so that the results of the formulas are fixed and can no longer change.
The current script I am using is:
function myFunction() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getActiveSheet();
var r = ss.getRange(1,2,ss.getLastRow());
var vals = r.getValues();
for(var i =0;i<vals.length;i++){
ss.getRange(i+1,2).setValue(ss.getRange(i+1,2).getValue());
}
}
However, in column B beneath the data set, all the cells start with IF(B2092="","",etc). On cells that are empty, I still require the formula to remain in place. I only want to script to be applied to cells that have data within.
Can anyone help with this? Thanks
Try this:
function myFunction() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getValues(); // get values
var formulas = rg.getFormulas(); // get formulas
// merge values & formulas
var data = formulas.map((row,row_index) =>
row.map((cel, cel_index) =>
{ return (cel) ? cel : vals[row_index][cel_index] }));
rg.setFormulas(data); // output
}
Corrected version:
function copy_values_and_formulas() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getValues(); // get values
var formulas = rg.getFormulas(); // get formulas
// merge values & formulas
var data = vals.map((row, row_index) =>
row.map((cel, cel_index) =>
{ return (cel == 0) ? formulas[row_index][cel_index] : cel }));
rg.setFormulas(data); // output
}
function myFunction() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getDisplayValues();
for(var i=0;i<vals.length;i++){
if(vals[i][1] && vals[i][1].length!=0) {
ss.getRange(i+1,2).setValue(ss.getRange(i+1,2).getValue());
}
}
}