Running App Script with output on a different Sheet - google-apps-script

I have found a script that pulls data from a cell and adds a row to the bottom of the Google Sheet, but I want to be able to have the output on a separate sheet called "Data". Here is the script:
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Channels");
var date = new Date();
var value =sheet.getRange("B9").getValue();
sheet.appendRow([date,value])
}
I know I need to change the sheet.appendRow but I don't know what I need to get it to another sheet, say in cell A2.

You must declare the second sheet.
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Channels");
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var date = new Date();
var value = sheet.getRange("B9").getValue();
dataSheet.appendRow([date,value])
}
See:
The Modern JavaScript Tutorial

Outputting on a different sheet
function recordValue() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Channels");
const dsh = ss.getSheetByName("Data")
dsh.getRange(dsh.getLastRow() + 1, 1, 1,2),setValues([[new Date(),sh.getRange("B9").getValue()]]);
}
setValues

Related

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.

Google Sheets - Copy row to bottom of data in the sheet each day

I'm trying to create a script to copy a row of data in google sheets to the bottom of a list
i.e. A2:L2 data gets copied and pasted to a new row at the bottom of the sheet. If there's no row available then it creates a new row and copies the data in there.
I'd then have this run on a time trigger at the end of each day
Any idea why this is not working?
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("PERFORMANCE");
var source = sheet.getRange("A2:L2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
}
This works for me:
function appendMyNewRow() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName("PERFORMANCE");
//const sh=ss.getSheetByName("Sheet1");
const shsr=2;
const vs=sh.getRange(shsr,1,1,sh.getLastColumn()).getValues();
vs[0][0]=new Date();
sh.appendRow(vs[0]);
}

Google Sheets Script - Copying data range from one sheet to another - whole range not copying

Bit of a rookie at this and learning atm. I am using the following script to copy data from one sheet to another in the same Google spreadsheet:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Cover");
var source = sheet.getRange("a1:c21");
var values = source.getValues();
var sheet2 = ss.getSheetByName("January 2020")
values [0] [0] = new Date();
sheet2.appendRow(values [0])
}
Which works fine to copy a single row but does not copy the entire range listed (a1:c21). Now I imagine the problem lies around Row 8 but I am not sure.
Here is the link to the spreadsheet I am using for trying this out:
https://docs.google.com/spreadsheets/d/15pEbNy-PmrGeAmpASRUK2iXIyJPfg43Mm2r6bbiTBuM/edit#gid=1296663899
function recordHistory() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Cover");
var source = sheet.getRange("a1:c21");
var values = source.getValues();
var sheet2 = ss.getSheetByName("January 2020")
values [0][0] = new Date();
sheet2.getRange(sheet2.getLastRow()+1,1,values.length,values[0].length).setValues(values);
}

Google Sheets Script hideSheet() , Sheet not hidden

Trying to duplicate a hidden template, causing it to be revealed, but then have it be hidden again after the duplication.
The hideSheet() function is not working. Any ideas?
function googleQuiz(){
//Duplicate Sheet
var ss =SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.setActiveSheet(ss.getSheetByName('QuizFormTPlate'));
ss.duplicateActiveSheet();
sheet.hideSheet();
//Rename sheet
var dSheet = ss.setActiveSheet(ss.getSheetByName('Copy of QuizFormTPlate'));
var date = new Date();
var tz = ss.getSpreadsheetTimeZone();
var marksDate = Utilities.formatDate(date, tz, 'dd-MMM');
var name = "G-Quiz ".concat(marksDate);
dSheet.setName(name);
//Insert Cell
var cell = dSheet.getRange("C2");
cell.setValue('Formative');
}
Actually it's "showSheet()"
This works for me:
var spreadsheet = SpreadsheetApp.getActive();
var sheetTemplate = spreadsheet.getSheetByName('Template');
sheetTemplate.showSheet();
//do something...
sheetTemplate.hideSheet();
Since hideSheet() takes no effect if the sheet is already hidden:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#hidesheet
Showing the hidden sheet first should do the trick:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#showsheet
Here is alternative solution that will that copy the sheet and keep it hidden
function googleQuiz(){
//Duplicate Sheet
var ss =SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('QuizFormTPlate');
var dSheet = sheet.copyTo(SpreadsheetApp.openById(ss.getId()))
dSheet.showSheet()
//Rename sheet
var date = new Date();
var tz = ss.getSpreadsheetTimeZone();
var marksDate = Utilities.formatDate(date, tz, 'dd-MMM');
var name = "G-Quiz ".concat(marksDate);
dSheet.setName(name);
//Insert Cell
var cell = dSheet.getRange("C2");
cell.setValue('Formative');
}
Basically, instead of your using duplicateActiveSheet() function of a spreadsheet object. You can use copyTo() function of a sheet object and provide it with the current spreadsheet.
sheet.copyTo(SpreadsheetApp.openById(ss.getId())).
And directly pass the new sheet object to var dSheet to make it visible and rename it.
It appears the issue is caused by using setActiveSpreadSheet on a hidden sheet, if you call ss.getSheetByName('QuizFormTPlate').showSheet(); prior to the ss.setActiveSpreadsheet(ss.getSheetByName('QuizFormTPlate')); the call to hideSheet() will work fine afterwards. Posting this here so anyone else running into this problem can just add the .showSheet() instead of reworking their whole code.