How to set daily google app script to output fixed value - google-apps-script

Am quite new with Google Scripts and wondering if anyone can help with this.
I've connected Google Analytics to Google Sheets (via add-on) and made settings so that conversion numbers are generated daily into a new google sheet tab (sheetB).
I'd like to match dates then copy the conversion data from sheetB to sheetA (master sheet which has acts like a dashboard).
The above can be done using this formula:
=IF(DA2=JuicerSessTime!$A$16,JuicerSessTime!$B$16)
The problem I'm facing is that since the above is a formula, I can't save the conversion data as a fixed value. So yesterday's value would show as FALSE from the formula above.
I'd like to be able to keep and save all daily generated values.
I've tried to create a google script below but it doesn't seem to work, hope someone can help.
function moveOneValuesOnlyToday() {
var mydate = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sdate = ss.getRange('sheetB!B2');
var sdate2 = ss.getRange('sheetA!DA2');
var source = ss.getRange('sheetA!DA5');
if (sdate == sdate2){
source.copyTo(ss.getRange('sheetA!DA6'), {contentsOnly: true});
}
}
Thanks in advance!

Related

Google Apps Script copies spreadsheet every time I run the script

I'm trying to write a script that executes a function every time I open the spreadsheet the script is linked to.
But now every time I run the function, a copy of the spreadsheet is created and that definitely shouldn't happen.
I'm not sure if it might have to do with the triggers or type of deployment I set. I put a screenshot of the trigger here.
As the type of deployment I put web app.
The code in the script is pretty simple because I didn't want to go forward yet when the error is still happening. It looks like this:
function mediaplan() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s_week = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Quick view - Week");
var s_month = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Quick view - Month");
var test = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var currentWeek = Utilities.formatDate(new Date(), "GMT", "w");
console.log(currentWeek);
}
It would be great if someone could tell me what I did wrong or if it's an error by Google.
Thanks in advance!

Google Sheets Apps Script to copy (GoogleFinance) data archive via appendRow

I'm using in Sheets the GoogleFinance-API to get the current $/€ exchange course.
I'm archiving this data via an apps script - reading out the fields and writing those back in the sheet using appendRow - for testing purposes I did it on a minute interval...
It worked for about 10 rows - after this it writes instead of the stock-exchange value a date '30.12.1899 23:41:33' in the field (the logs in apps scripts are fin though) - have no clue where it comes from and what I'm doing wrong here - please have a look at the screen grabs
function saveData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var data = sheet.getRange(5,1,1,2).getValues();
var time = data[0][0];
var stock = data[0][1];
Logger.log(time+" ::: "+stock);
sheet.appendRow(data[0]);
Logger.log(data);
}
just added the images here - stackoverflow is not very intuitive for 1st time users.

Is it even possible to copy data from one sheet to another with a script? ImportRange style?

Ive searched for 2 days, can't seem to find the answer or anyone that is even doing something kinda like this. I am trying to bring my employees sheets over to my "Master sheet" with a script. I need to keep the formatting and the notes in the cells.
Best I can find is I can open their spread sheets and copy it to my master, but for 8 employees this will be a pain if I need to do this multiple times a day. I would like to just build a function that I can run my script from the "master sheet" and have it write over the last data in their tab. But I want to be able to do this from within my master sheet. I am the owner of all the sheets but this is becoming a pain.
function copyCell() {
var os = SpreadsheetApp.openById('1n4iFXGuC7yG1XC-UIbuT9VrQ7rJWngPkDCv0vsvDed4');
var sheets = os.getSheets();
var existingNote;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var jobnumber = sheets.getRange("C2:C").getValues();
for (var i = 0; i < jobnumber.length; i++)
if (jobnumber[i][0] == sheets){
existingNote = sheets.getRange("C" + (i+2)).getNote();
sheets.getRange("C" + (i+2)).ss.setNote(existingNote);
}
};
Someone please let me know if it is possible and maybe reference a script I can see to be able to modify to fit my needs. Thanks for any help.
This is a possible solution for copying the notes associated with a range of cells. And pasting them onto a similar-sized range of cells.
To copy the notes you can use getNotes(). Docs here.
To paste the notes you can use setNotes(). Docs here.
Ths sample code in this documentation is pretty good if you need to add it into your script.
If you have a script you'd like to share am happy to help you make it work properly.
Edit
Something like this:
function transferNotes() {
// Get notes (not comments) from the source spreadsheet
var sourceSpreadheet = SpreadsheetApp.openById('ID_of_source_spreadsheet');
var sourceTab = sourceSpreadheet.getSheetByName('Name_source_tab');
var sourceRange = sourceTab.getRange('Source_range_address'); // e.g. "A:D"
var notes = sourceRange.getNotes();
// Post the notes to the target spreadsheet
var targetSpreadheet = SpreadsheetApp.openById('ID_of_target_spreadsheet');
var targetTab = targetSpreadheet.getSheetByName('Name_target_tab');
var targetRange = targetTab.getRange('Target_range_address'); // Should be the same dimensions as sourceRange
targetRange.setNotes(notes);
}

Convert onEdit script to manually running script

The script has the purpose of writing the datetimestamp to a specific cell the moment when a cell in column 4 has the text Assign.
I have a script that theoretical works but I'm getting my data from Appsheet. The problem with this is that Appsheet writes data into my sheet but the script wont see it as edited cells, so it wont write the time stamp.
But my knowledge about Apps Scripts is pretty bad. And I am getting errors with the source line and the col, val lines.
function onEdit(e) {
var sh = e.source.getActiveSheet();
var row = e.range.getRow();
var col = e.range.getColumn();
var val = sh.getRange(row, 4).getValue();
//check if sheet is 'Blad1' & value is 'Assign'
if (sh.getSheetName() === 'Blad1' && val == 'Assign') {
var tz = e.source.getSpreadsheetTimeZone();
var date = Utilities.formatDate(new Date(), tz, 'dd-MM-yyyy hhmmss');
//set date in column 14 for same row
sh.getRange(row, 14).setValue(date);
}
}
I want to convert my script to a manually run script with a time-based trigger of 1 min. That way I hope the script will see the changed cell to Assign.
The problem is rooted in apps script triggers restrictions, specifically in their inability to listen to script-based events. I would suggest deploying your script as a WebApp with doGet() / doPost() functions to listen to API requests and, if I am correct in assuming that AppSheet works similarly to Zapier, etc., add a step that calls your WebApp after making changes to your Spreadsheet.
Please, see this guide on WebApps, its pretty straightforward to follow.
P.s. Btw, refrain from creating triggers acting as event listeners, you will easily cap your quotas!

Can I add a formula to a google form response using Apps Script?

Apologies as I am a beginner to coding. I am interested in using Google Apps Script to automate the analysis of a Google Form response.
The simple example I have is for the spreadsheet of responses for a form asking people:
1) how many players there were?
2) where they finished [1st, 2nd, etc,]
On submission of the form I want to run a script that calculates how may points they received and inserts this value in the next available column (column E in this example).
I have tried writing my first Apps Script to automate this process, but without success.
SAMPLE CODE:
function onFormSubmit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses Master");
var players = e.values[2];
var place = e.values[3];
var positionPoints = e.values[4];
var positionPoints = (players - place + 1);
return positionPoints;
}
I know there are workarounds available by creating duplicate pages, but I was hoping someone might be able to advise me on how to code a solution in App Scripts, in the hope I might get a better understanding of the coding process.
You can write your appscript in the response collector spreadsheet itself instead of writing your code in form's script editor.
So, go to your response sheet and paste this code.
function myFunction()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses Master");
var rowNo = sheet.getLastRow();
var colNo = sheet.getLastColumn();
sheet.getRange(rowNo, colNo).setValue("=(C"+rowNo+"-D"+rowNo+")+1");
}
Now, go to Resources -> Current project triggers. Click on add new and set these values in drop downs: myFunction - From Spreadsheet - On form submit.
And you are done. Whenever a new response is submitted, position points will be calculated automatically.
Here,
variable sheet gets your active spreadsheet for different sheet operations which you can perform.
rowNo and colNo as seen in the code, simply fetches the value of last row/column respectively of spreadsheet in which something is written.
And, to set formula in column 'E', you can use setValue. Hence, "=(C"+rowNo+"-D"+rowNo+")+1" will be converted to (C2-D2)+1 in case of second row and so on for next rows.
getRange is simply used to tell the script that write formula inside particular cell.