How to send email form google sheets? - google-apps-script

I want to create a script in apps script , which will send me an email once a day if today's date is in which cell.I want there to be a line header in the email.
Where is a code that tried.
function sendEmailOnDate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
range = ss.getRange("task!C3:G8");
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
if (cell.getValue() == new Date()) {
MailApp.sendEmail("mymail#gmail.com", "Nowa wiadomość", "The date in cell A1 is today");
}
}
Spreeadsheet name = task
I'm. also tired to put the current date in first cell A1.But It didn't work.
I got an error = Range not found

Try this:
function sendEmailOnDate() {
const ss = SpreadsheetApp.getActive();
const sheet = ss.getSheets()[0];
const dt = new Date(sheet.getRange("A1").getValue());
const dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf()
if (dtv == new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf()) {
MailApp.sendEmail("mymail#gmail.com", "Nowa wiadomość", "The date in cell A1 is today");
}
}

Related

Apps Script to Append data AND add date dd-MM-yyyy format

I am trying to combine two Apps Scripts.
Append data - this works fine and adds data according to a time frame
function appendOvertime() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var reportSheet = ss.getSheetByName("Weekly Numbers");
var recordsSheet = ss.getSheetByName("Append Data")
var reportData = reportSheet.getRange("A2:O25")
.getValues();
var lastRow = recordsSheet.getLastRow();
//copy data
recordsSheet.getRange(lastRow + 1, 2, 24, 15)
.setValues(reportData);
}
Add date - this works for the google sheet I have created to test it.
var SHEET_NAME = 'Sheet1';
var DATETIME_HEADER = 'datetime';
function getDatetimeCol(){
var headers =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME).getDataRange().getValues().shift();
var colindex = headers.indexOf(DATETIME_HEADER);
return colindex+1;
}
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var cell = ss.getActiveCell();
var datecell = ss.getRange(cell.getRowIndex(), getDatetimeCol());
if (ss.getName() == SHEET_NAME && cell.getColumn() == 1 && !cell.isBlank() && datecell.isBlank()) {
datecell.setValue(new Date()).setNumberFormat("dd-MM-yyyy");
}
I want the date script to run after the data has been appended in the sheet called "Append Data" and for the date to be placed in Column A - which has the column header "Date"
I am new to apps script so would appreciate any help anyone can offer. Thanks
I can get the scripts to work independently
I cannot get the date placed in a column before where edits are being made i.e. add data to B but date in A
I want the date script to run after the data has been appended in the sheet called "Append Data" and for the date to be placed in Column A - which has the column header "Date"
One way to do that is to put the necessary code at the end of appendOvertime(), like this:
function appendOvertime() {
const ss = SpreadsheetApp.getActive();
const reportData = ss.getRange('Weekly Numbers!A2:O25').getValues();
const recordsSheet = ss.getSheetByName('Append Data')
const appended = appendRows_(recordsSheet, reportData, 2);
recordsSheet.getRange(appended.rowStart, 1, appended.numRows, 1)
.setValue(new Date()).setNumberFormat('dd-MM-yyyy');
}
For that to work, you will need to paste the appendRows_() and getLastRow_() utility functions in your script project.

Running App Script with output on a different Sheet

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

Attempting to copy an active sheet and open a new sheet but with only specific cells?

I am trying to add to my script to copy only certain cells from a template I have set up. Currently I have a menu item that creates a "copy" of the template sheet, but it is currently copying everything. I am also no pro at this and have only been trying to figure out this coding system for a couple of days. Any help would be wonderful. Below is the script I currently have.
function onOpen() {
var menu = [{name: "Copy Template", functionName: "newSheet"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("New Sheet", menu);
}
function newSheet() {
var user = Session.getEffectiveUser().getEmail().split("#")[0];
var sheetName = formatDate()+' '+user;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getActiveSheet();
ss.insertSheet(sheetName, 1, {template: templateSheet});
}
function formatDate() {
var month, day, d = new Date();
month = ('0'+(d.getMonth()+1)).slice(-2);
day = ('0'+d.getDate()).slice(-2);
return d.getFullYear()+'-'+month+'-'+day;
}
function onEdit() {
var user = Session.getEffectiveUser().getEmail().split("#")[0];
var ss, date;
ss = SpreadsheetApp.getActiveSpreadsheet();
date = new Date();
ss.getActiveSheet().getRange('j2').setValue(date).setNumberFormat('H:mm');
ss.getActiveSheet().getRange('i2').setValue(user);
}
You can probally use range:
function newSheet() {
var user = Session.getEffectiveUser().getEmail().split("#")[0];
var sheetName = formatDate()+' '+user;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getActiveSheet();
var getRange =SpreadsheetApp.getActiveSpreadsheet().getSheetByName([TEMPLATE_SHEET_NAME]).getRange('YOUR RANGE Ex A1:A10').getValues();
....
....
....
}
And after use again get the range where you want them and use SetValues(getRange)

Missing ; error in first attempt at script

I am writing an OnOpen script for a spreadsheet.
I have a number in cell B1 and a date in C1
When the spreadsheet is opened, I want to check that the date on the sheet is today's and if not, it will change the sheet's date and also reset the number in B1 to 0
I am getting the error Missing ; before statement. (line 8, file "Code")
(Which is the If Statement)
function OnOpen(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Create Ticket");
var TicketCount = sheet.getRange("B1").getvalue();
var TicketDate = sheet.getRange("C1").getvalue();
var Today = date().setNumberFormat("dd/mm/yyyy");
If not TicketDate = Today {
sheet.getRange("C1").setValue(new Date()).setNumberFormat("dd/mm/yyyy");
sheet.getRange("B1").setvalue(0);
}
}
This is all new to me so my apologies if this is basic. The only coding I ever did was simple VBA, so I don't even know what == means.
You can try this function
function onOpen(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Create Ticket");
var TicketCount = sheet.getRange('B1').getValue();
var TicketDate = Utilities.formatDate(new Date(sheet.getRange("C1").getValue()),Session.getScriptTimeZone(),"dd/MM/yyyy");
var Today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"dd/MM/yyyy");
if(TicketDate != Today) {
sheet.getRange("C1").setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"dd/MM/yyyy"));
sheet.getRange("B1").setValue(0);
}
}
Be carefull because in your code there is some typo error.
setvalue => setValue
getvalue => getValue
This will generate error.
To format date you can use Utilities.formatDate()
Stéphane

Google Spreadsheet opening the last edited cell using script

Using the following code allows the last edited cell to open when reopening a google spreadsheet.
function setTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction").forSpreadsheet(ss).onEdit().create();
}
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var sName = sheet.getName();
var currentCell = sheet.getActiveCell().getA1Notation();
UserProperties.setProperty("mySheetName", sName);
UserProperties.setProperty("myCell", currentCell);
}
function onOpen() {
var lastModifiedSheet = UserProperties.getProperty("mySheetName");
var lastModifiedCell = UserProperties.getProperty("myCell");
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(lastModifiedSheet).getRange(lastModifiedCell).activate();
}
I have a cell which has
=today()
Which updates so the code doesn't open the last row of my spreadsheet, but opens in the cell that updates with today().
How would I update the script to open the spreadsheet at my last edited cell in the last row of the spreadsheet?
Can you try this:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var sName = sheet.getName();
var currentCell = sheet.getActiveCell().getA1Notation();
var temp = "";
if (currentCell != "A1") {temp = currentCell;}
UserProperties.setProperty("mySheetName", sName);
UserProperties.setProperty("myCell", temp);
}
I added a temp variable and am doing a check (assuming only A1 has the today() function) before updating temp. Seems to work.