Dupulicate Script for Google Sheets - google-apps-script

I have a Google sheet that I need to copy each day into a new tab. I need the new tab to be named with the new date and I need all of the formatting duplicated. I've been able to get it to make a new tab and rename it correctly but I cannot get it to transfer the formatting over. The cell range I need is A1:G34. Can anyone help?
I've tried doing a macro recording and it still didn't work

I believe this will do the trick. You just need to change sheets name from 'Main' to which you want to copy data from and click on ⏩️ > Duplicate at top of page next to Help.
/** #OnlyCurrentDoc */
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('⏩️')
.addItem('Duplicate', 'duplicateSheet')
.addToUi();
}
function duplicateSheet(){
var ss = SpreadsheetApp.getActive();
var ssh = ss.getRange("Main!A1:G34"); //Selects sheet and range you wish to copy to new sheet
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy"); // Todays date
ss.insertSheet(date); //Creates new sheet with todays date as name
var dsh = ss.getSheetByName(date);
var drg = dsh.getRange(dsh.getLastRow() + 1, 1);
ssh.copyTo(drg); //Cpoies A1:G34 data from Main sheet to new sheet
}

Related

Apps Script - Creating a Spreadsheet then copy all the value and paste it in a different spreadsheet

I'm trying to to do the following tasks using Apps Script:
Create a new spreadsheet. The name will be "My spreadsheet" + Date of the day
Write a random value in the new spreadsheet
Copy the entire sheet (1st tab) from the new spreadsheet
Paste the value in another spreadsheet (in a specific sheet)
Here is the script I've written so far:
function copyPasteAllData() {
var date = Utilities.formatDate(new Date(), "GMT+7", "dd/MM/yyyy");
// get today's date
var ss = SpreadsheetApp.create("Existing Data - Apps Script - "+ date);
// create a new spreadsheet
var ssId = ss.getId();
// get ID of new spreadsheet
var existing = Sheets.Spreadsheets.Values.get('MyspreadsheetID', "Existing Data");
// get existing Spreadsheet + sheet location
var newSheet = Sheets.Spreadsheets.Values.get(ssId, "Sheet1");
// get new Spreadsheet + sheet location
ss.getRange('A1').setValue('1')
// setup a random value
var rangeAllData = newSheet.getRange(1, 1, newSheet.getMaxRows(), newSheet.getMaxColumns());
// copy the entire sheet from the
existing.rangeAllData.setValue()
}
I think the line below need to be changed but I'm not sure how to fix this.
I'm still quite a beginner with Apps Script and coding in general, apologies if it seems obvious.
var existing = Sheets.Spreadsheets.Values.get('MyspreadsheetID', "Existing Data");
var newSheet = Sheets.Spreadsheets.Values.get(ssId, "Sheet1");
Feel free to change anything in the script. Just note that I can only get the sheet ID and not the name
Thank you
Updated from the comment below:
In this case you can use the method getDataRange() to get the data from the new Sheet.
Then on the destination you can use the same range from the new sheet or use the one you want from the old sheet:
function copyPasteAllData() {
//getDate
var date = Utilities.formatDate(new Date(), "GMT+7", "dd/MM/yyyy");
//Create new sheet + set value
var newSheet = SpreadsheetApp.create("Existing Data - Apps Script - " + date);
newSheet.getRange('A1').setValue('this is new');
//get new sheet DataRange
var source = newSheet.getId();
var newSheetDataRange = SpreadsheetApp.openById(source).getSheets()[0].getDataRange().getA1Notation();
Logger.log(newSheetDataRange)
var newSheetValues = SpreadsheetApp.openById(source).getSheets()[0].getDataRange().getValues();
Logger.log(newSheetValues)
//copy the data to the old sheet
SpreadsheetApp.openById('OLD SHEET ID').getSheets()[0].getRange(newSheetDataRange).setValues(newSheetValues);
}

Timing out issue in Google Sheet

The code below is used to perform some simple functions in a Google Sheet.
It creates two new sheets (makes them (0) and (1), names them, and adds color) and hides another sheet.
The code has been extremely unreliable. If I create a blank google sheet and test it works fine but when I add it to the live version of the sheet it times out and fails to run all the code.
I also tested copying the live version (in case there was some corruption in the file) and initially it seemed to work but then the same problem occurred. I am brand new to app script so perhaps I am missing something obvious. I would appreciate any suggestions.
function New_Tabs() {
var spreadsheet = SpreadsheetApp.getActive();
var curDate = Utilities.formatDate(new Date(), "GMT+1", "M/d") //sets format for current
date as month/day
//Selects and activates the Day_date sheet / Gets calculated date info
var sheet = SpreadsheetApp.getActive().getSheetByName('Day_Date');
sheet.activate();
var val = SpreadsheetApp.getActiveSheet().getRange(2,6).getValue();
//Logger.log(val) Used this to check the value was pulled correctly
//Insert the sheet for today's date - Data from portal/Excel macro will be pasted here
spreadsheet.insertSheet(0); //Makes it the first sheet
spreadsheet.getActiveSheet().setName(val); //pulls calculated date from Day_Date sheet
spreadsheet.getActiveSheet().setTabColor('#00ff00'); //Colors the sheet tab green
//Insert the sheet for Logistics
spreadsheet.insertSheet(1); //Makes it the second sheet
spreadsheet.getActiveSheet().setName("Logistics " + curDate ); //names the sheet with text
and current date
spreadsheet.getActiveSheet().setTabColor('#00ff00'); //Colors the sheet tab green
//trying to slow down to make hiding the tab more reliable
Utilities.sleep(2000);// pause for 2 seconds
//selects the sheet for today's date - Data from portal/Excel macro will be pasted here
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(val), true);
//trying to slow down to make hiding the tab more reliable
Utilities.sleep(2000);// pause for 200 milliseconds
//selects the Day_date sheet and hides it
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Day_Date'), true);
spreadsheet.getActiveSheet().hideSheet();
SpreadsheetApp.getUi().alert("Completed");
};
Try it this way:
function New_Tabs() {
const ss = SpreadsheetApp.getActive();
const curDate = Utilities.formatDate(new Date(), "GMT+1", "M/d");
const sh = SpreadsheetApp.getActive().getSheetByName('Day_Date');
const val = sh.getRange(2,6).getValue();
let ns = ss.insertSheet(0);
ns.setName(val);
ns.setTabColor('#00ff00');
let ns1 = ss.insertSheet(1);
ns1.setName("Logistics " + curDate );
ns1.setTabColor('#00ff00');
ns.hideSheet();
ss.toast("Completed")
}

How do I clear data only but not formulas on google sheets?

I am building a daily checklist that uses a trigger to make a new tab every night at 12pm. The code is below - but I need the code to clear the content of those cells, not the underlying formulas. I assume instead of "var rangetoclear" it must be something else, but I could not find on help page.
Can anybody help?
Here is the code:
/* DASHBOARD---------------------------------------------------------------------------------------------------
Edit this section to change the dates tabs are created for and the range of data that will be cleared each time! */
var numberOfDaysForwardForNextTab = 0
var rangeToClear = 'F8:I1003'
// -------------------------------------------------------------------------------------------------------------
var ss = SpreadsheetApp.getActiveSpreadsheet(); // get the spreadsheet object
SpreadsheetApp.setActiveSheet(ss.getSheets()[0]); // set the first sheet as active
// Sets date for add tab & date for delete tab
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
var today = new Date();
var addWeek = Utilities.formatDate(new Date(today.getTime() + (numberOfDaysForwardForNextTab * MILLIS_PER_DAY)), "GMT+1", "MM-dd-yy");
// Adds tab
ss.duplicateActiveSheet(); //Copies current sheet
ss.renameActiveSheet(addWeek); //Renames sheet to date from above
ss.moveActiveSheet(1); //Moves sheet to the first position
// Prepares tab (clears old content)
var sheet = ss.getSheetByName(addWeek);
sheet.getRange(rangeToClear).clearContent()
}
One solution is to get the formulas of the source sheet and for the range you want to clear rangeToClear and then copy the formulas to the duplicate sheet you just created after clearing its contents:
const rangeToClear = 'F8:I1003';
const source_sheet = ss.getSheets()[0];
const formulas = source_sheet.getRange(rangeToClear).getFormulas();
const sheet = ss.getSheetByName(addWeek);
sheet.getRange(rangeToClear).clearContent()
sheet.getRange(rangeToClear).setFormulas(formulas)
References:
getFormulas()
setFormulas()

In there a way to save a constantly changing value once a day and store it in google sheets?

I am currently using a google sheet to manage my portfolio. I want to save the value of my portfolio every day and save those numbers to create a growth graph. I want to take the value from one cell, paste it to another and the next day grab the value again from the same cell and paste on the cell under the one of the previous day without loosing my previous value. I am trying to create an AppScript script but I cant figure out how to go to the next cell once the day changes.
function Paste() {
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
var cellNumber = 3;
var cell = "C" + cellNumber;
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange(cell).activate();
spreadsheet.getRange('J11').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
I want to increase cellNumber once a day!
function Paste() {
var ts=Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
var srcss=SpreadsheetApp.openById('sssid');//source of information spreadsheet
var desss=SpreadsheetApp.openById('dssid');//storage destination spreadsheet
var srcsh=srcss.getSheetByName('source sheet name');
var dessh=desss.getSheetByName('destination sheet name');
var value=srcsh.getRange('C3').getValue();
dessh.appendRow([ts,value]);
};

Active Sheet Doesn't Change in Google Sheets Mobile App

I'm using a spreadsheet to track my routine in the gym. I have it set up to duplicate the active sheet and name the duplicate with today's date. It's set up to trigger the below code on edit:
function myProgress() {
var fullDate = Utilities.formatDate(new Date(), "GMT-5", "dd/MM/yyyy");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var name = fullDate;
var currentName = ss.getSheetByName(name);
if (!currentName) {
ss.insertSheet(name, {template: sheet});
ss.moveActiveSheet(1);
ss.setActiveSheet(ss.getSheets()[0]);
}
}
Which works great on desktop.
If today (28/02/2020) I edit my workout routine done on 25/02/2020, Sheet 25/02/2020 is duplicated and named "28/02/2020". Then Sheet 28/02/2020 is the active sheet.
But because of the way the mobile app works (maybe to do with the confirmation of entered text?) it will duplicate and rename the the sheet but it won't set the new one as the active sheet.
Yes, it's only a minor inconvenience to have to remember to go to today's sheet before continuing to edit, but I'd really love to work out a way around this.