I am quite new on this platform and I am starting also to learn Google App script. I have issues a formula I am trying to insert in App script which is not working. I am trying to extract the week number, month and year and paste them automatically in their columns at each data entry (userform)
I used this formula but it doesn't work
Sorry to mix it up with a bit of french but here is the code:
// Boucle pour la semaine or Loop for the week
//var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Base Données Production");
//ss1.getRange( lastRow_Basedonprod1, 11 ).setFormula( '=IFERROR(VLOOKUP(A&+lastRow_Basedonprod1,Utilitaire!$C$21:$H$386,4),"")' );
//ss1.getRange( lastRow_Basedonprod2, 11 ).setFormula( '=IFERROR(VLOOKUP(A&+lastRow_Basedonprod2,Utilitaire!$C$21:$H$386,4),"")' );
//ss1.getRange( lastRow_Basedonprod3, 11 ).setFormula( '=IFERROR(VLOOKUP(A&+lastRow_Basedonprod3,Utilitaire!$C$21:$H$386,4),"")' );
//ss1.getRange( lastRow_Basedonprod4, 11 ).setFormula( '=IFERROR(VLOOKUP(A&+lastRow_Basedonprod4,Utilitaire!$C$21:$H$386,4),"")' );
Basically what I am trying to do is every time I enter values in "Saisie production" tab, I am getting those values in a new sheet called "Base données Production". It's working fine except for the week, month and year which are in columns K,L,M.
Google sheet link:
https://docs.google.com/spreadsheets/d/1F4-nzozPpmgP_QUtnUlbcnJPTgs-q7EmpPhuU2iUREk/edit?usp=sharing
You will need to concatenate the value in your variables into the formula string. For example:
var lastRow_Basedonprod1 = 1; // Assuming that the value is 1
var formula = '=IFERROR(VLOOKUP(A' + lastRow_Basedonprod1 + ',Utilitaire!$C$21:$H$386,4),"")';
console.log( formula );
So your formula logic should look like this
ss1.getRange( lastRow_Basedonprod1, 11 ).setFormula( '=IFERROR(VLOOKUP(A'+ lastRow_Basedonprod1 + ',Utilitaire!$C$21:$H$386,4),"")' );
ss1.getRange( lastRow_Basedonprod2, 11 ).setFormula( '=IFERROR(VLOOKUP(A'+ lastRow_Basedonprod2 + ',Utilitaire!$C$21:$H$386,4),"")' );
ss1.getRange( lastRow_Basedonprod3, 11 ).setFormula( '=IFERROR(VLOOKUP(A'+ lastRow_Basedonprod3 + ',Utilitaire!$C$21:$H$386,4),"")' );
ss1.getRange( lastRow_Basedonprod4, 11 ).setFormula( '=IFERROR(VLOOKUP(A'+ lastRow_Basedonprod4 + ',Utilitaire!$C$21:$H$386,4),"")' );
Related
The script places the dates correctly on my Google Calendar, then misplaces them all one day early, then places the last one correctly again.
input from google sheet, displayed in Google Calendar
1/4/23 -> 1/4/23 put in goog cal correctly
1/8/23 -> 1/7/23 - one day off
2/5/23 -> 2/4/23 - one day off
6/18/23 -> 6/17/23 - one day off
10/15/23 -> 10/15/23 - correct
function addEvents(){
var ss =
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var cal =
CalendarApp.getCalendarById("#group.calendar.google.com");
var data = ss.getRange("A1:C" + lr).getValues();
for(var i = 0; i<data.length;i++){
cal.createAllDayEvent(data[i][0], new Date(data[i][1]),
{description:'PRO: ' + data[i][2]});
}
}
three columns in the google sheet
title, date, and description
Try GetDisplayValues()
var data = ss.getRange("A1:C" + lr).GetDisplayValues();
and potentially format the column as yyyy-MM-dd.
This will ensure the data the script is using is the same as you see on the sheet. Formatting the column will ensure new Date() converts the the data correctly.
If its still off add a couple hours to your Date object to ensure the time is firmly in the middle of the day.
let today = new Date();
today.setHours(today.getHours() + 4);
I am writing a script to manage product manufacturing dates.
Here is how the script works:
It modifies the format of the columns to be sure that they are dates (tab "Extraction")
It deletes the duplicates of this tab
He creates a new tab named "Dates" in which he pastes the dates of the tab "Extraction" (only the columns B and D).
He displays in column C the dates of column B - 1 day (if in B2 it is 20/05/2022, he copies in C2 the 19/05/2022).
I managed to write all these parts but I would like for step 4 to calculate according to the working days and bank holidays.
For example if the date in B2 is Monday 16 May 2022, I would like it to display in C2 Friday 13 May 2022. And if for example Friday May 13, 2022 is a bank holiday, I would like it to display Thursday May 12, 2022 in C2 instead.
So I created a tab listing all the bankholidays (same name) but I can't figure out how to write this part of the script.
What can I try next?
Here is the script:
function miseEnFormeDates() {
// Déclaration des constantes et des variables
const classeur = SpreadsheetApp.getActiveSpreadsheet();
const feuille = classeur.getSheetByName("Extraction");
var range = feuille.getRange('A:E');
var tDates = []; var nbDates = 0; var tDonneesDateFab = []; var tDonneesDateDDM = [];
// ----- RECOPIAGE DES DATES DANS UN OUVEL ONGLET ----- //
// Format Date
range.setNumberFormat('dd/MM/yyyy');
// Suppression des doublons
range.removeDuplicates().activate();
// Ajout d'une nouvelle feuille nommée "Dates"
yourNewSheet = classeur.insertSheet();
yourNewSheet.setName("Dates");
// Recopiage des dates de l'onglet "Extraction"" vers l'onglet "Dates"
tDates = feuille.getRange("A2:A").getValues();
nbDates = tDates.filter(String).length;
const feuilleDates = classeur.getSheetByName("Dates");
tDonneesDateFab = feuille.getRange("D2:D"+(nbDates+1)).getValues();
tDonneesDateDDM = feuille.getRange("B2:B"+(nbDates+1)).getValues();
feuilleDates.getRange("A1").setValue("Date fab.").setBackground("#33cccc").setFontWeight("bold").setFontColor("white").setHorizontalAlignment('center').setVerticalAlignment('middle');
feuilleDates.getRange("B1").setValue("DDM").setBackground("#33cccc").setFontWeight("bold").setFontColor("white").setHorizontalAlignment('center').setVerticalAlignment('middle');
for (let i= 0; i < nbDates; i ++){
feuilleDates.getRange("A"+(2+i)).setValue(tDonneesDateFab[i]).setHorizontalAlignment('center').setVerticalAlignment('middle');
feuilleDates.getRange("B"+(2+i)).setValue(tDonneesDateDDM[i]).setHorizontalAlignment('center').setVerticalAlignment('middle');
}
// ----- AFFICHAGE DE DATE -1 ----- //
feuilleDates.getRange("C1").setValue("Date -1").setBackground("#33cccc").setFontWeight("bold").setFontColor("white").setHorizontalAlignment('center').setVerticalAlignment('middle');
for (let i= 0; i < nbDates; i ++){
var date = new Date(tDonneesDateDDM[i]);
var result = new Date();
var dateEnNombre =result.setDate(date.getDate()-1);
dateFinale = new Date(dateEnNombre);
feuilleDates.getRange("C"+(2+i)).setValue(dateFinale).setHorizontalAlignment('center').setVerticalAlignment('middle');
}
}
And here is the link to the file if you want to see how it looks like.
----- EDIT -----
I’ll try to explain better my problem and minimalize it.
I’ve a Sheets where i take dates and report them in an other tab of this same Sheets.
It’s work perfectly but now, i would like to have in a separate column the day before the dates who re reported.And if these dates reported are in week-end or a bank holiday, i would like the day before.
I’ll give you two exemples for be as understandable as possible.
My date is monday 23 may 2022, i would like to have wednesday 20 may 2022 reported.
And if wednesday 20 may 2022 is a bank holiday, i would like to have thursday 19 may 2022 reported in the next cell.
I have a tab with all of the bank holidays and, if it’s possible i would like ask all of them each time to know if the “day before” is a bank holiday
This question already has answers here:
concatenate date and time in google spreadsheet
(3 answers)
Closed 1 year ago.
I have my working hours in a Sheets document that I want to use to make Events in Calendar.
Following the tutorial:
https://cloud.google.com/blog/products/g-suite/g-suite-pro-tip-how-to-automatically-add-a-schedule-from-google-sheets-into-calendar
I have this script:
function scheduleShifts() {
/**
Task 1) Open the event calendar
**/
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange("D1").getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
/** Task 2) Pull each shift information into the code, in a form that the code can understand
**/
var signups = spreadsheet.getRange("E2:F100").getValues();
/**
Create the calender entry
**/
for (x=0; x<signups.length; x++) {
var shift = signups[x];
eventName = "Work hours";
var startTime = shift[0];
var endTime = shift[1];
eventCal.createEvent(eventName, startTime, endTime); }
}
But, I get the error:
Exception: The parameters (String,String,String) don't match the method signature for CalendarApp.Calendar.createEvent.
scheduleShifts # Code.gs:24
Because I do not work everyday, there are blank cells in the data range from Sheets, could this be the issue?
And how can I ask the script to ignore those cells within that data range that are blank and NOT to create an event for them?
My work hours are provided with times and dates in seperate columns, so I used this Sheet formula to convert the data to DateTime format, and placed into new cells in columns E and F:
=IF(C2 <> "",concatenate(text(A2,"dd/mm/yyyy")&" "&text(C2,"hh:mm:ss")),"")
=IF(D2 <> "",concatenate(text(A2,"dd/mm/yyyy")&" "&text(D2,"hh:mm:ss")),"")
A
B
C
D
E
F
29/11/2021
man
13:30
22:30
29/11/2021 13:30:00
29/11/2021 22:30:00
30/11/2021
tir
01/12/2021
ons
02/12/2021
tor
09:30
16:42
02/12/2021 09:30:00
02/12/2021 16:42:00
03/12/2021
fre
09:30
16:42
03/12/2021 09:30:00
03/12/2021 16:42:00
04/12/2021
lør
05/12/2021
søn
06/12/2021
man
09:30
16:30
06/12/2021 09:30:00
06/12/2021 16:30:00
07/12/2021
tir
09:30
18:30
07/12/2021 09:30:00
07/12/2021 18:30:00
My understanding is using .getValues ignores the formula and only delivers the value of the cell, so I don't believe this to be the issue, but I thought I had better mention it for full disclosure.
The method is createEvent(String,Date,Date)
You are sending createEvent(String,String,String)
You need to parse your sting into a date.
// Creates an event for the moon landing and logs the ID.
var event = CalendarApp.getDefaultCalendar().createEvent('Apollo 11 Landing',
new Date('July 20, 1969 20:00:00 UTC'),
new Date('July 21, 1969 21:00:00 UTC'));
Logger.log('Event ID: ' + event.getId());
I’ve created a script that exports campaign results to a Google Sheets file. When I implemented the script on September 29th, the data was exported perfectly. See example below (notice that the last four numbers are right-aligned because they're digits).
Month
AccountDescriptiveName
CampaignName
Impressions
SearchImpressionShare
TopImpressionPercentage
AbsoluteTopImpressionPercentage
2019-03-01
Account name
Campaign name
2800
90.82%
0.73
0.73
However, something changed in the output two weeks ago. The columns TopImpressionPercentage and AbsoluteTopImpressionPercentage were exported differently.
Month
AccountDescriptiveName
CampaignName
Impressions
SearchImpressionShare
TopImpressionPercentage
AbsoluteTopImpressionPercentage
2019-03-01
Account name
Campaign name
2800
90.82%
0.73
0.73
The issue here is that TopImpressionPercentage and AbsoluteTopImpressionPercentage have different cell properties. Google Sheets doesn’t see these cells as digits but as text (I guess). Now the goal of this export is that we automatically make calculations in a different sheet based on these number. These automatic calculations fail now because these two columns don’t work anymore. Manually changing the cell properties in Google Sheets is not a solution.
I have no idea how this has changed. I haven’t made any changes to the script. I’ve tried to run the script to a different Google Sheets file but without any difference.
Here's the script. My apologies for the incorrect indenting, but the focus here is on the elements that the script contains.
function main(){
var sheetURL = 'URL';
var tabName = 'Tabname';
var QUERIES = [{'query' : 'SELECT Month, AccountDescriptiveName, CampaignName, Labels, '
+ 'Impressions, Clicks, Cost, Conversions, SearchImpressionShare, TopImpressionPercentage, AbsoluteTopImpressionPercentage '
+ 'FROM CAMPAIGN_PERFORMANCE_REPORT '
+ 'WHERE Impressions > 0 '
+ 'DURING 20190101,20220101',
'spreadsheetUrl' : sheetURL,
'tabName' : tabName,
'reportVersion' : 'v201809'
}
];
var query;
var spreadsheetUrl;
var tabName;
var reportVersion;
for(var i in QUERIES) {
var queryObject = QUERIES[i];
query = queryObject.query;
spreadsheetUrl = queryObject.spreadsheetUrl;
tabName = queryObject.tabName;
reportVersion = queryObject.reportVersion;
}
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
var sheet = spreadsheet.getSheetByName(tabName);
var report = AdWordsApp.report(query, {apiVersion: reportVersion});
report.exportToSheet(sheet);
}
I was forwarded from the Google Ads Scrips Direct Support as everything works as it should in the Campaign Performance Report. Because "the Google Ads Scripts utilizes Google Apps Script's Spreadsheet to actually extract the report to your spreadsheet", they recommended me reaching out to Apps Script team.
I hope that you are able to help me out.
Thank you in advance.
You can easily parse the values such that they end up matching the type you need for your calculations.
The thing with Apps Script is that when using getValue, the method will end up returning a value of type Number, Boolean, Date, or String, depending on the value of the cell. Therefore, if the cell is indeed formatted to store values of type text, the value returned in the script will be of type String.
To fix this, simply do the following:
var topImpressionPercentage = Number(rangeTop.getValue());
var absoluteTopImpressionPercentage = Number(rangeAbs.getValue());
Reference
Apps Script Range Class - getValue().
THrough Table Capture extension I have copied a html table from a webpage into the google spreadsheet.
In that table, there is a column in decimals for ex :
12.4
I want to change this into
12:40:00
If I normally replace . with : then it's replacing to
12:04:00
You should give a try with Format => Number => Time
You should open a new script by Tools=> Script Editor and take a look at that which is a tutorial to delete row and you'll need to adapt it to change row values. You'll find all the needed doc here
And your code may look like this :
for each row{
var string = new array(2);
//Split the string a the .
string = row.split(".");
// if the second part is like 4 in 12.4 you set it to 40
if (string[1].lenght() == 1)
string[1] += 0;
// Set the row value to the format you like, here : 12:40:00
row.setValue(string[0] + ":" + string[1] + ":00");
}