I am trying to set a formula in Google Spreadsheet F2:F if E2:E has a date. If E2:E is blank, then no formula is set to F2:F.
My Code so far. Any Help or suggestions would be much appreciated.
function formula(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var date = Utilities.formatDate(new Date(), "America/New_York", "MMMM dd, yyyy");
var colE = sheet.getRange("E2:E");
var colF = sheet.getRange("F2:F");
if (colE != date){
colF.setFormula("=round(abs(E2:E-NOW()),-1)");
} else {
colF.setValue("");
}
}
Here is how to do it...
function formula(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var colE = sheet.getRange("E2:E").getValues();
var colF = sheet.getRange("F2:F").getFormulas();
for(var n in colE){
if (typeof(colE[n][0])=='object'){
colF[n][0] = "=round(abs(E"+n+":E-NOW()),-1)";
} else {
colF[n][0] = "";
}
}
sheet.getRange('F2:F').setFormulas(colF);
}
Related
I am a newbie with coding and am trying to get the minimum value of a column (of a temporary sheet) and then copy that value and some data from the corresponding row to another sheet. For some reason it seems to work only once during the loop, is it because I'm trying to define a range based on an object?
function create_filter(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet1 = ss.getSheetByName("APperCountry");
const sheet2 =ss.getSheetByName("Tender");
var lanelist = sheet2.getRange(2, 1, sheet2.getLastRow(), 8).getValues();
var country = ('B:B');
var origin = ('E:E');
for(var i=2; i<=lanelist.length;i++){
Logger.log("Filter has been added.");
country = ('B'+i);
Logger.log(country);
var origin = ('E'+i)
var calc = ('M'+i);
var apname = ('N'+i);
const ori = sheet2.getRange(origin);
if(ori.isBlank()){
continue;
}
const range = sheet1.getRange("A:D");
const filter = range.createFilter();
var Ctry = sheet2.getRange(country).getValue();
const Filter_Criteria1 = SpreadsheetApp.newFilterCriteria().whenTextContains(Ctry);
const coll1 = 1;
const add_filter1 = filter.setColumnFilterCriteria(coll1,Filter_Criteria1);
const new_sheet = ss.insertSheet();
new_sheet.setName("AirportDistanceCalc");
var tempsheet = ss.getSheetByName('AirportDistanceCalc');
range.copyTo(new_sheet.getRange(1,1));
var aplist = new_sheet.getRange(2, 1, new_sheet.getLastRow()-1, 8).getValues();
new_sheet.getRange(1,5,1,1).setValue("Origin")
new_sheet.getRange(1,6,1,1).setValue("DistanceKM")
for(var j=0; j<aplist.length;j++){
Logger.log(origin);
ori.copyTo(new_sheet.getRange(j+2,5));
mainFun()
}
let comparisonRange = tempsheet.getRange("F2:F");
let comparisonRangeValues = comparisonRange.getValues().filter(String);
let minimum = comparisonRangeValues[0][0];
comparisonRangeValues.forEach((rowItem, rowIndex) => {
comparisonRangeValues[rowIndex].forEach((columnItem) => {
minimum = Math.min(minimum, columnItem);
});
});
console.log(minimum);
sheet2.getRange(calc).setValue(minimum);
tempsheet.getRange(minimum, 2).copyTo(sheet2.getRange(apname));
if (tempsheet) {
ss.deleteSheet(tempsheet);
}
filter.remove();
}
}
The minimum of a column
function mincolumn(col=1) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet1");
const min = sh.getRange(1,col,sh.getLastRow()).getValues().flat().filter(e => !isNaN(e)).sort((a,b) => a - b)[0];
Logger.log(min);
}
I have a problem.
How to get the filename from the url?
enter image description here
I wouldn't normally do this since you haven't shown us what you've tried, but I'm feeling generous.
This function should work for you. (Note that you'll need to grant permissions for it to run.)
function getFileNames() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Get_File_Name");
var links = sheet.getRange("A2:A").getValues();
var filenames = [];
for (var i = 0; i < links.length; i++) {
var url = links[i][0];
if (url != "") {
var filename = SpreadsheetApp.openByUrl(links[i][0]).getName();
filenames.push([filename]);
}
}
var startRow = 2; // print in row 2 since row 1 is the header row
var fileNameColumn = 2; // Column B = column 2
var destination = sheet.getRange(startRow, fileNameColumn, filenames.length, filenames[0].length);
destination.setValues(filenames);
}
Another way
function getFileNames() {
var driveApp = DriveApp;
// SET THE SHEET HERE
var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
//SET THE URL LINK COLUMN HERE : From row 2 since row 1 is the header row till last row
var links = sheet.getRange("P2:P").getValues();
var filenames = [];
for (var i = 0; i < links.length; i++) {
var fileId = getIdFromUrl(links[i][0]);
if (fileId != "" && fileId != null) {
var getfile = DriveApp.getFileById(fileId);
var filename = getfile.getName();
Logger.log(filename);
filenames.push([filename]);
} else {
filenames.push([""]);
}
}
// SET STARTING ROW TO PRINT: From row 2 since row 1 is the header row
var startRow = 2;
// SET WHICH COLUMN TO PRINT : Column A = column 1 / Column B = column 2
// MAKE SURE THE SHEET LAST COLUMN HEADER IS FILLED + 1 (next column)
var fileNameColumn = sheet.getLastColumn() + 1;
var destination = sheet.getRange(startRow, fileNameColumn, filenames.length, filenames[0].length);
destination.setValues(filenames);
}
function getIdFromUrl(url) { return url.match(/[-\w]{25,}/); }
You can create a custom function in spreadsheets like this.
function getSSName(name) {
var ss = SpreadsheetApp.openByUrl(url);
return ss.getName();
}
I've the following excel which needs to be transposed to columns. I have tried using the built-in formula and hte pivot table but that did not help me much since the data is in a single column and I am looking for transposing to multiple columns.
Excel: (all data in single column)
ABC1
F1
D1
Sym1
ABC2
See Link2
ABC3
F3
D3
Sym3
ABC4
See Link4
ABC5
See Link5
The output should be like:
ABC Functions Description Sym See Link
ABC1 F1 D1 Sym1
ABC2 See Link1
ABC3 F3 D3 Sym3
ABC4 See Link4
ABC5 See Link5
See that when 'See Link' is present for the row data, none of the functions, descriptions and syms are present and vice versa.
So I figured this out. Used G script in googlesheet to get this right. For anyone looking for code, here's it:
function run() {
var inputSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var outputSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Description");
var rows = new Array(new Array());
var values = inputSheet.getRange(1, 1, inputSheet.getLastRow()).getValues();
Logger.log(values.length);
var j = 0;
var endOfRow = false;
var singleRow = new Array();
for (var row in values) {
for (var col in values[row]) {
var value = values[row][col];
if(value.indexOf("F") != -1) {
singleRow[1] = value;
}
else if(value.indexOf("D") != -1) {
singleRow[2] = value;
}
else if(value.indexOf("See Link") != -1) {
singleRow[4] = value;
endOfRow = true;
}
else if(value.indexOf("Sym") != -1) {
singleRow[3] = value;
endOfRow = true;
}
else {
singleRow[0] = value;
}
}
if(endOfRow) {
rows[j] = singleRow;
j++;
endOfRow = false;
var singleRow = new Array();
}
}
for(var d=2; d < rows.length; d++) {
var singleRow = rows[d];
for(var f = 0; f < singleRow.length; f++) {
if(singleRow[f] == undefined) {
outputSheet.getRange(d, f+1).setValue("");
} else {
outputSheet.getRange(d, f+1).setValue(singleRow[f]);
}
}
}
}
I just started using Google Sheets and I'm having trouble when setting a script.
I need a cell (let's say "AF4") to get the last modification date of another cell (let's say "X4").
Here's my script so far:
`function onEdit(e)
{
var sheet = SpreadsheetApp.getActiveSheet();
var editRange = sheet.getActiveRange();
var editRow = editRange.getRow();
var editCol = editRange.getColumn();
var range = sheet.getRange("X4");
var rangeRow = range.getRow();
var rangeCol = range.getColumn();
if (editRow = rangeRow && editCol = rangeCol)
{
sheet.getRange("AF4").setValue(new Date());
}
}`
When I try to save it, it says "invalid transfer on left side, line 1, "code" "
I'd really apprecite any help with that.
Thank you,
CURTY
The syntax for your condition seems to be wrong, that's why the error appears :)
if (editRow = rangeRow && editCol = rangeCol)
in case you want to check based on more conditions, the syntax must be
if (condition) {
block of code to be executed if the condition is true
if (condition) {
block of code to be executed if the condition is true
}
}`
I modified a bit of your code that you get an idea, of what needs to be done in order to achieve your goal, this script to get the last modification date of column X and post "new date" into AF4 (so whenever you edit something on column "x" and the row 4 the date will be set in AF4)
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var editRange = sheet.getActiveRange();
var editRow = editRange.getRow();
var editCol = editRange.getColumn();
var editCell = sheet.getActiveCell();
var range = sheet.getRange("X4");
var rangeRow = range.getRow();
var rangeCol = range.getColumn();
if( sheet.getName() == "Sheet1" ) { //checks that we're on the correct sheet
if( editCell.getColumn() == 24 ) { //checks that we're on the correct column ; column x = 24
if( editCell.getRow() == 4 ) { //checks that we're on the correct row ; row 4 = 4
sheet.getRange("AF4").setValue(new Date());
SpreadsheetApp.getActive().toast('Data changed', 'Info', 2); //give a Info, that something was done correctly
}
}
}
}
Hope this will help you!
EDIT
Script from comments request, whenever something on Column X is changed, add Date to column AF in the same row
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var editRange = sheet.getActiveRange();
var editCol = editRange.getColumn();
var editCell = sheet.getActiveCell();
var range = sheet.getRange("X4");
if( editCell.getColumn() == 24 ) {
var rangeCol = range.getColumn();
var editRow = editRange.getRow();
sheet.getRange(editRow,rangeCol+8).setValue(new Date()); //use the RangeCol + 8 Columns
SpreadsheetApp.getActive().toast('Date added', 'Info', 2); //this line can be deleted
}
}
I have many buttons on my sheet, to hide and unhide some rows.
function hide1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
sheet.hideRows(24,11);
}
function unhide1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
sheet.showRows(24,11);
}
I have also created a script to duplicate this sheet, in order to create a new one to be filled.
function clonemastersheet() {
var sheetc = SpreadsheetApp.getActiveSpreadsheet()
var getname = sheetc.getRange("t10").getValue();
var name = getname
var pp = SpreadsheetApp.getActiveSpreadsheet();
var mastersheet = pp.getSheetByName('Form0').copyTo(pp);
mastersheet.setName(name)
pp.setActiveSheet(mastersheet)
PROBLEM:
When it duplicates the new sheet buttons dont work because the macros are related to the first one.
I need to create a script that will be duplicated with the sheet and work with them.
Is it possible?
function hide1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.hideRows(24,11);
}
function unhide1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.showRows(24,11);
}