Converting Formula Result in cell into Notes - google-apps-script

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])
});
}

Related

Google sheets script to export rows to different worksheet

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

Search and Add characters to Google sheet value array with GAS?

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);
}

How to reset a certain column values to zeros?

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;
}

How to overwrite cell function with Paste value only using Google Script

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());
}
}
}

How to auto select range from a filter without having to manually enter it?

I am wondering if there is a way to auto select the cell range based on selected filter.
Example:
Set Filter in (Column H)
Auto select the result cell data starting Column A (A2000): Column C (C5000) etc. --- This is where I am getting stuck. I do not know how to write to auto select the result cell data based on the selected filter.
Currently, my workout is to manually enter the cell so I could move on with writing the codes. I hope I am making sense above.
---- Code----
function ColHtoActive() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Current');
var crange = sheet.getRange('A7:I7350').activate();
var currentCell = sheet.setCurrentCell(sheet.getRange('H7');
var hSfilter = sheet.getRange('A7:I7350').createFilter();
var ccC1 = sheet.getRange('H7').activate();
var cCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues('Inactive']).build();
sheet.getFilter().setColumnFilterCriteria(8, cCriteria);
}
function copycolA() {
var ss = SpreadsheetApp.getActive().getSheetByName('A');
ss.getRange('A2307').activate();
ss.getRange('A2307:A7155').copyTo(
ss.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL,
false);
}
You can get the filtered range dimensions from getFilter().getRange(). This will copy all the filtered range:
function copycolA() {
var sourceSheet = SpreadsheetApp.getActive().getSheetByName('Current');
var targetSheet = SpreadsheetApp.getActive().getSheetByName('A');
var sourceRange = sourceSheet.getFilter().getRange();
sourceRange.copyTo(
targetSheet.getRange('A1'),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL,
false);
}
To read:
Filter#Range
Related answer
Note that .getValues() or other script operations will NOT get the filtered only, but all of the values.
Possibly
function myFilter() {
const ss = SpreadsheetApp.getActiveSpreadsheet(),
sht = ss.getSheetByName("Current"),
rng = sht.getDataRange(),
rawData = rng.getDisplayValues();
let filterValues = ["Inactive"],
col = 8, // column "H".
out = rawData.filter(dataFilter);
out = [rawData[0], ...out]; //Add headers to filtered data
function dataFilter(arr) {
return filterValues.includes(arr[col-1]);
}
const osh=SpreadsheetApp.getActive().getSheetByName("A");
osh.clear();
osh.getRange(1,1,out.length,out[0].length).setValues(out);
}