How to get number of sheet from list - google-apps-script

What script should do. First, get sheet names from range on sheet List_1. Then, using this array of names, put some data only on these sheets.
https://docs.google.com/spreadsheets/d/1Xdws6Vr6YVXuj19mvpOvkpJokOfY2T7Sh1aMpous_Xw/edit#gid=0
function VList() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheetNames = ss.getSheetByName("List_1");
var rangeNames = sheetNames.getRange("B3:B5").getValues();
for (var sheetId = 0; sheetId<sheets.length; sheetId++){
if(sheets[sheetId].getSheetName() == rangeNames) {
ss.getRange("C1").setValue("done");
}
}
}
Problem. When script starts - nothing happens. How to get number of sheet and vlookup only on them? Maybe, exist some function like "getSheetId" which can get number of sheet.
Solved, thanks to bruce.
function VList() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheetNames = ss.getSheetByName("List_1").getRange("B3:B5").getValues();
sheetNames.forEach(function(row) {
var sheet = ss.getSheetByName (row[0]);
if (!sheet) throw 'missing sheet ' + row[0];
sheet.getRange("c1").setValue ("done");
});
}

getValues() returns a two dimensional array, which you then compare with a string value - so that will never be true.
Once you fix that, you will still be updating the same cell, C1 with 'done' multiple times - ss points to the List_1 sheet - I assume you mean to update the sheet whose name is in the list.
Why not just use the list in b3:b5 to simply get each sheet by name and update it? There's no need to go through the sheets list since you already know their names.
something like
ss.getSheetByName("List_1").getRange("b3:b5").getValues()
.forEach(function(row) {
var sheet = ss.getSheetByName (row[0]);
if (!sheet) throw 'missing sheet ' + row[0];
sheet.getRange("c1").setValue ("done");
});

Related

I am trying to only activate the worksheet of which the value is in a specific cell

I have a formula in worksheet 'Price History' cell E21 that changes to different worksheet names automatically depending on what other cells are equaling too. The script I have is returning TypeError: newSheetName.getRange is not a function. In the below script i tried to copy a blank cell above E21 and paste the cell into a blank cell on the specified worksheet but that didn't help. I am trying to only activate the worksheet of which the value is in E21. Thank you
function MyAccount() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Price History");
var newSheetName = sheet.getRange("E21").getValue();
sheet.getRange("E20").copyTo(newSheetName.getRange("C1"), { contentsOnly: true });
}
This line var newSheetName = sheet.getRange("E21").getValue(); doesn't set the variable newSheetName as a sheet but a string value.
SUGGESTION:
Since you have mentioned that the value of cell E21 basically contains worksheet name that is randomly changing, perhaps you can still try using getRangeByName(name) method as this will return the sheet with the given name & then you can get the range of that sheet. See this sample tweaked script below:
UPDATED
function MyAccount() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Price History");
var newSheetName = ss.getSheetByName(sheet.getRange("E21").getValue());
var Direction=SpreadsheetApp.Direction;
var lastRowOfC = ss.getRange("C"+(ss.getLastRow()+1)).getNextDataCell(Direction.UP).getRow() + 1; //Get the last row of column C on any worksheet
sheet.getRange("E20").copyTo(newSheetName.getRange(lastRowOfC,3), { contentsOnly: true });
}
Getting the last row script was derived from one of the answers from Determining the last row in a single column (Getting last row on a column that contains empty gaps)
Sample test on my end:
NOTE: It would also be better if you could share a sample sheet that you're using in case I have misunderstood your setup.
UPDATED
After running the script, the test cell value was copied to the destination sheet called NewTestSheet on the last row of column C, which is C4
You are trying to use a String value as a Sheet. Instead, use the sheet name to get a new sheet, then copy to the new range.
function MyAccount() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Price History");
var copyRange = sheet.getRange("E20");
var nameRange = sheet.getRange("E21");
copyRange.copyTo(ss.getSheetByName(nameRange.getValue()).getRange("C1"), { contentsOnly: true });
}
Or,
function MyAccount() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Price History");
var copyValue = sheet.getRange("E20").getValue();
var nameRange = sheet.getRange("E21");
ss.getSheetByName(nameRange.getValue()).getRange("C1").setValue(copyValue);
}

google sheets copy formatting from active sheet to every other sheet

I really need your help. After 2 days, I'm still on "ZERO".
I need a script that copies all the formats from an active sheet to every other sheet.
It would be nice to have the conditional formatting, alternating colors and data validation too.
I think it's not so hard, but I'm really new in script writing.
This is the sheet:
https://docs.google.com/spreadsheets/d/1mOkm_r4rngPXTkIerSQJKxRih31sM7XoZCZVnoHUc5M/edit?usp=sharing
The code so far:
enter function copyFormatting(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var copyTo = ss.getSheetByName('agent2');
var range = copyTo.getRange(1, 1, copyTo.getMaxColumns(), copyTo.getMaxRows());
sheet.getBandings()[0].copyTo(copyTo.getRange(1,1)),SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
}
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
copyFormatting(); // the function what yo like to run
}
})
}
I was able to figure out and write some functions: clear formatting on all sheets, format row headers on all sheets, show a list of sheets, but this one is too hard for me.
Even if I try to model it first with the macro recorder.
I would be very grateful, if you could help me with this issue.
I was able to figure it out:
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
var sampleSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var maxRow =sampleSheet.getMaxRows();
var maxCol =sampleSheet.getMaxColumns();
var sampleRange = sampleSheet.getRange(1,1,maxRow,maxCol);
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
var targetSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var targetRange = targetSheet.getRange(1,1);
sampleRange.copyTo(targetRange, {formatOnly:true}) // it copies the formatting
}
})
}
Reference
Copy To
Try insertSheet(options) and if that doesn't work the try copyTo(destination, options)

Insert new sheet using sheet template is not working

I have a sheet names in column A of the active sheet.
I am trying to get each new tab to be a duplicate of my Template tab, but they are just blank.
Here is the code:
/** #OnlyCurrentDoc */
function makeTabs(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var last = sheet.getLastRow();
for(var i = 1; i < last ; i++){
var tabName = sheet.getRange(i+1,1).getValue();
var templateSheet = ss.getSheetByName("Template");
var create = ss.insertSheet(tabName,{template: templateSheet});
}
}
Can anyone tell why created sheets are not based on template?
Solution:
Here is what you are looking for:
function copySheets() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const last = sheet.getLastRow();
const template = ss.getSheetByName('Template');
const sh_names = sheet.getRange('A2:A'+last).getValues().flat([1]);
sh_names.forEach((name)=>{
const copy=template.copyTo(ss);
copy.setName(name);
})
}
Output:
Explanation:
I am using forEach() to iterate over the list of the names in column A of the active sheet. For each name, I am creating a copy of the template with this code:
const copy=template.copyTo(ss);
copy.setName(name);
Please keep in mind that you need to run this function when you have selected the active sheet (see my screenshot); the sheet that contains the name of the sheets you want to create. That is the reason I believe it is safer to get a sheet based on its name to be sure that you are getting the right input:
const sheet = ss.getSheetByName('Sheet1');
Obviously, you can't run this function more than once; it will output an error that sheets with these sheet names already exist.
Last but not least, all the duplicate sheets will have exactly the same content and format of Template sheet. I assume based on your question that this is your goal.

Auto Fill Cell Script

I have a sheet with two columns. A Date and Assist.
The sheet is filled out with a Date and with a number of how many assist.
What I would like is for the Assist column to automatically insert a 0 if there is a date entered and only if there is no number already in the Assist column.
I have the following script so far and it is triggered onOpen.
function storeValue() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// ss is now the spreadsheet the script is associated with
var sheet = ss.getSheets()[0]; // sheets are counted starting from 0
// sheet is the first worksheet in the spreadsheet
var cell = sheet.getRange("J3:J");
cell.setValue(0);
}`
This is a little slow, but works. Adapt as needed. It currently assumes your data are in columns A & B.
function storeValue() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// ss is now the spreadsheet the script is associated with
var sheet = ss.getSheets()[0]; // sheets are counted starting from 0
var dataRange = sheet.getRange("A:B")
for (var row = 1; dataRange.getNumRows(); row++) {
if(sheet.getRange(row,1).getValue() != ''){
if(sheet.getRange(row,2).getValue() == ''){
sheet.getRange(row,2).setValue(0);
}
}
}
}

Add data to a spreadsheet from another using script.

I iterate through the rows and get the value of a certain cell like so:
for (var i = 2; i < lastRow; i++) {
if (sheet.getRange(i, getColIndexByName("Department Concerned")).getValue() == "HR") {
// Do some stuff
}
}
I copy data to another sheet like so:
// select the Spreadsheets
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("Spreadsheet ID");
// select the Sheets from the Spreadsheets
var source_sheet = ss.getSheetByName("Logs");
var target_sheet = target.getSheetByName("Sheet3");
// select the range from the Sheets
var source_range = source_sheet.getRange("A2:J500");
var target_range = target_sheet.getRange("A2:J500");
// get the values from the source sheet
var values = source_range.getValues();
// set the values to the destination sheet
target_range.setValues(values);
What I need is to copy that data and add it to the same range of the other Spreadsheet depending on the cell value that I just took in my "for loop".
Thank you.
What you think was right, but need to find better approach.
Select values from source sheet
Check each rows, column Department Concerned for "HR" value
Select rows that fulfill above condition
Paste it in target sheet
I combined those scenarios as this,
function myFunction()
{
// select the Spreadsheets
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("Spreadsheet ID");
// select the Sheets from the Spreadsheets
var source_sheet = ss.getSheetByName("Logs");
var target_sheet = target.getSheetByName("Sheet3");
// select the range from the Sheets
var source_range = source_sheet.getRange("A2:J500");
// get the values from the source sheet
var values = source_range.getValues();
// an array for selected rows
var selectedRows = [];
for(var row in values)
{
// thinking 5th column is Department Concerned
if(values[row][4] =='HR')
{
//add add entire row to selected 2d array
selectedRows.push(values[row]);
}
}
// set the values to the destination sheet
target_sheet.getRange(2, 1, selectedRows.length,10).setValues(selectedRows);
}