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
Related
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.
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");
}
}
I am writing a very simple macro in Google Sheets to get the month value and write it in a cell on the sheet. But for some reason the month value is not getting written to the cell.
function Macro1() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet1'), true);
var today = new Date;
var prevMonth = Utilities.formatDate(today, 'GMT+1', 'MMMM yy');
var cell = spreadsheet.getRange('G12');
cell.setValue = prevMonth;
Logger.log(prevMonth);
Logger.log(cell.getValue());
};
I can see the value of prevMonth in the log. It is correct but no value is getting written in the actual cell and hence, the last line also returns blank.
And yes, I have read-write access on the sheet.
setValue is a function, so you need to pass parameter to it.
function Macro1() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet1'), true);
var today = new Date();
var prevMonth = Utilities.formatDate(today, 'GMT+1', 'MMMM yy');
var cell = spreadsheet.getRange('G12');
cell.setValue(prevMonth);
Logger.log(prevMonth);
Logger.log(cell.getValue());
}
cell.setValue(prevMonth);
after some hours of reading documentation and other answers I couldn't solve my problem.
I'm trying to trigger a formula when one cell is edited. I get it to run when I just ask a value to be set (see commented line in the code) but not when I call another formula.
Here's the script:
function onEdit() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var column = cell.getColumn();
if ( row ===1 && column ===4 && sheet.getName() ==="Hoja 1" ) {
sendData();
//sheet.getRange(1,1).setValue("5");
}
function sendData() {
var sheet = SpreadsheetApp.openById("1pxS9F9YEagzzRyTOqIDGVpDaCEiY_Z1DOYxq1QVzWGk");
var source_sheet = sheet.getSheetByName("Hoja 1");
var target_sheet = sheet.getSheetByName("Hoja 2");
var range1 = source_sheet.getRange(1, 7, 6, 1).getValues();
target_sheet.getRange(1,1,6,1).setValues(range1);
}
}
You have to pass the sheet into the called function this way:
sendData(spreadsheet);
function sendData(sheet) {
...
}
onEdit() is a simple trigger. Simple triggers cannot access services that require authorization. So it cannot call SpreadsheetApp.openById().
https://developers.google.com/apps-script/guides/triggers#restrictions
And pay attention -- in the function sendData() you're using the name sheet but it's a spreadsheet actually. This is not an error but it can be confusing a bit.
Struggling beginner here, trying to get my script done and I'm just lost at this point. Let me start by posting my script.
function dailyclear(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Today');
sheet.copyTo(ss)
var nusheet = ss.getSheetByName('Copy of Today')
var value = nusheet.getRange("B2").getCell(1,1).getValue()
SpreadsheetApp.setActiveSheet(nusheet)
Logger.log("Current index of sheet: %s", nusheet.getIndex())
ss.moveActiveSheet(2);
nusheet.setName(value);
//Clear "Today"
var lastrow = sheet.getLastRow()
sheet.deleteRows(8, lastrow-7)
//Set Date Value for "Today"
var dd = Utilities.formatDate(new Date(), "GMT-5", "MM/dd/yy");
sheet.getRange("B2").getCell(1,1).setValue(dd);
}
What I want my script to do is before the //Clear line have an If/else statement that goes like this..."IF there is any text in A8 THEN run sheet.deleterows(8, lastrow-7), ELSE skip to setting the date." I hope that makes sense. Basically I want to to delete all rows except the top 7 based off if any data has been entered in A8, otherwise don't try to delete anything because it will just error on me and lock up.
I have been trying for days to figure this out on my own just by reading on the net and I am throwing in the towel. Any help would be appreciated. The link to my sheet is https://docs.google.com/spreadsheets/d/1NZxXjMptx6ldzL2CKk6OwJcq_heIv995ZGVeIE59xNc/edit?usp=sharing
Thanks for anything and everything offered!
I think this is what you're looking to achieve:
function dailyclear() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
var sheet = ss.getSheetByName('Today');
var getB2 = sheet.getRange("B2").getValue();
sheet.copyTo(ss).setName(getB2);
var nusheet = ss.getSheetByName(getB2);
Logger.log("Current index of sheet: %s", nusheet.getIndex());
ss.setActiveSheet(nusheet);
ss.moveActiveSheet(2);
var getA8 = sheet.getRange('A8').getValue();
if (getA8) {
var numRows = sheet.getMaxRows();
if (numRows > 8) {
sheet.deleteRows(8, numRows - 8);
} else {
sheet.deleteRow(8);
}
} else {
var newDate = Utilities.formatDate(new Date(), "GMT-5", "MM/dd/yy");
sheet.getRange("B2").setValue(newDate);
}
}