I have created a google sheet that contains a button that when clicked will create a random number in a particular column. However, I need the code to find the first empty cell in the column for the random number. The code I am currently using fills all the cells in the row verses one cell at a time when the button is clicked.
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [ {name:"Resubmit",functionName:"Resubmit"} ];
sheet.addMenu("Script", entries);
};
function Resubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var cell = sheet.getRange("A:A");
cell.setValue( Math.floor((Math.random()*1000000)+1) );
};
You can be more specific and give it a number of cells you want, as per docs:
getRange(row, column, numRows, numColumns)
You can append after the last row either by setting cell like this:
var cell = sheet.getRange(sheet.getLastRow() + 1, 1);
or by changing the function to this:
function Resubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");;
sheet.appendRow([Math.floor((Math.random()*1000000)+1)])
};
Related
Is there a way to take the Title Text from the Google Sheets cell?
So whenever I change the cell string then it should appear in the Chart automatically.
For example: I have May - 2021 in cell A1. Can I reference this in Title Text, like using =A1 or =Sheet1!A1? But it does not work.
I got this code, but how do I reference the cell into the code?
function onOpen() {
SpreadsheetApp.getUi().createMenu('Charts').addItem('Update', 'myFunction').addToUi();
}
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var chart = sheet.getCharts()[0];
chart = chart.modify()
.setOption('Summary!Q123', sheet.getActiveCell().getValue() || 'Summary!Q123')
.build();
sheet.updateChart(chart);
}
Although it is not possible to link a cell value as title, there is a trick that may solve your problem by creating a merge cell, camouflage, as the title. Then it will be changed, based on the text value you enter :)
Script:
You may use the following script to update the title by referring to A3 every time you execute the code. Basically, you are almost correct:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var newtitle = sheet.getRange(3, 1).getValue();
var charts = sheet.getCharts()[0];
var chart = charts.modify()
.setOption('title', newtitle)
.build()
sheet.updateChart(chart)
var scnCharts = sheet.getCharts()[1];
var chart2 = scnCharts.modify()
.setOption('title', newtitle)
.build()
sheet.updateChart(chart2)
}
I have this script here, but I want it to only run on ONE specific sheet named "Nes Smart Data" (SheetNo5). Currently it runs on all sheets and puts the data on cells where I don't want them to be. Can you help me correct the code? Thanks a lot!
function onEdit(e){
var sheet = SpreadsheetApp.getActiveSheet();
var range = e.range;
var column = range.getColumn();
var row = range.getRow();
var text;
if (column==11) { //Replace 2 with the column number of your comments cell//
var newRange = sheet.getRange(row,column-5); //If the date is in the next column
var today = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'dd.MM.yyyy');
newRange.setValue(today);
}
if (column == 11){
text = sheet.getRange(row, 23).getValue();
sheet.getRange(row, 23).setValue(text + e.value+".");
}
}
Replace the following variable and things should work as desired -
Current:
var sheet = SpreadsheetApp.getActiveSheet();
Modification proposed:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Nes Smart Data');
Let me know if it doesn't!
Certain cell in rows have hyperlink. need hyperlink to follow row when copied from sheet to sheet based on other cell value
function moveToTab() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var range = ss.getActiveRange();
var col = range.getColumn();
Logger.log(col);
var aSheet = ss.getActiveSheet();
var header = aSheet.getRange(10,col,1,1).getValue();
Logger.log(header);
if (header.toLowerCase() !== 'issued to') return;
var target = range.getValue();
if (target.toString().trim() === '') return;
if (target.toString().toLowerCase() === aSheet.getName().toLowerCase()) return;
var sheets = ss.getSheets();
var tSheet = sheets.filter(function(sheet) {
if (sheet.getName().toLowerCase() == target.toLowerCase()) return true;
else return false;
})[0];
if (!tSheet) return;
var row = range.getRow();
var values = aSheet.getRange(row, 1, 1, aSheet.getLastColumn()).getValues();
tSheet.appendRow(values[0]);
aSheet.deleteRow(row);
I know I am missing something simple. In my work book column "F" has a hyperlink to files. I am using the above script to move certain rows from sheet to sheet based on value in Column "K" and would like the hyper link in the cell in column "F" to follow teh row from sheet to sheet. What am i missing
I am getting this error: TypeError: Cannot call method "getName" of null. I have used a script to replace the formulas of the active sheet with the cell values. However, I don't want to accidentally run this on a few sheets, so I want to be able to exclude or include only specific sheets. I have been following another post and came up with this, and now I have the error:
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
//loop through all sheets and get name to see if it includes specific string
for (i = 0; i < sheets.length; i++) {
var sheet = ss.getSheetByName(sheets[i]);
var name = sheet.getName();
if (name.includes("String_00")) {
//get active sheet and replace range with values
var sheetActive = ss.getActiveSheet();
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
} else {
continue;
//skip over all those that don't meet the condition
}
}
}
UPDATE:
Trying this:
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
//loop through all sheets and get name to see if it includes specific string
for (i = 0; i < sheets.length; i++) {
//var sheet = ss.getSheetByName(sheets[i]);
var name = sheets[i].getName().toString();
if (name.indexOf("String_00") > -1) {
//get active sheet and replace range with values
//var sheetActive = ss.getActiveSheet();
var range = sheets.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
} else {
continue;
//skip over all those that don't meet the condition
}
}
}
But now it doesn't do anything. using .include was giving me an error, so I moved over to .indexOf() > -1. Does not freeze data as expected for any sheet, regardless of name.
I came up with 2 solutions for this, sort of started from the drawing board. One creates an array, a sort of blacklist, and then if the sheet name contains any item in the blacklist, then it returns and doesn't execute the replacement. The second solution searches for a pattern in the sheet name and only then continues on to the replacement. Both accomplish what I was trying to do. I think the code I found was a bit buggy and was a bit complicated for what I was doing. I didn't need to loop through all sheet names, just check the sheet name against given variables. Anyway, here they are:
This one creates the array blacklist of pages not to edit:
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ['Sheet1', 'Sheet2', 'Sheet3', 'Sheet4'];
var sheetActive = ss.getActiveSheet();
if (sheets.indexOf(sheetActive.getName()) > -1) return;
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
}
This one only searches for specific text in the sheet name before proceeding with the replacement.
function freezeValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetActive = ss.getActiveSheet();
var name = sheetActive.getName()
if (name.indexOf("String_00") > -1) {
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
}
}
I'm sure there is a method to use sheet index value so if you want to skip the first 4 sheets, you can probably do that as well, so it doesn't call on names at all.
UPDATE
function freezeValues2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetActive = ss.getActiveSheet();
if (sheetActive.getIndex() > 4) {
var range = sheetActive.getRange("A1:Z50");
range.copyTo(range, {contentsOnly: true});
}
}
screenshot
Hi all, i need help and i am not a coder. I am trying to achieve the same thing on sheet number 2.
My datas are imported through "=Submission!$b2" from sheet 1
i need help removing rows automatically when a specific cell on column H does not contain the value "Bekreft", i tried both codes shown here with no success.
This is what i added for script:
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('DATA - Do Not Touch!!!'); // change to your own
var values = s.getDataRange().getValues();
for(var i=values.length;i>0;i-=1){
var lcVal=values[i-1][0].toLowerCase() //Change to all lower case
var index = lcVal.indexOf("vent"); //now you only have to check for contains "vent"
if (lcVal.indexOf("vent") > -1){
s.deleteRow(i)};
}}
Seeing as you state you are "not a coder", and the code you pasted will not help you if you are referencing data from another page, I would suggest using a filter function to achieve your goal. You would add the following formula to your second page:
=FILTER(Submission!B:B,ISNUMBER(SEARCH("Bekreft", Submission!H:H)))
If you are looking to have a script go through your static list and delete out values that do not contain "Bekreft" then you can use the following script.
function onEdit() {
var sheet = SpreadsheetApp.openById("Add Sheet ID").getSheetByName('Sheet1');
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
//row for condition
if (row[7].indexOf("Bekreft")) {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};