How to check a google spreadsheet before processing a form? - google-sheets

I have tickets I'm selling for an event, and I have a google spreadsheet with a list of email addresses that should get a discount.
I'd like to check the google sheet once a user types in their email address in the form, and then if theirs is in our list, the discount gets applied.
I can't seem to figure out how to do this. The example code here: https://developers.google.com/sheets/api/quickstart/js requires users to allow access to their google drive, which isn't what I need. I just need to check my own spreadsheet.
How might I do this?

You can use Apps Script (since you're using Forms), if you want a handy solution. I don't know the complexity of your usecase but I'll just demo that this is doable:
So this is my spreadsheet, you can see the names on Column A:
name(0,0)
floyd(1,0)
conor(2,0)
john
carmack
borja
adam
I'm going to look for "adam"
function findPerson() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getSheets()[0].getRange("A1:A7").getValues();
for(var i = 0; i < range.length ; i++){
Logger.log(range[i]);
if( range[i] == "adam"){
Logger.log("FOUND YAH!");
}
}
}
And there, I found him! ;)
If that's a Google Form you're using, the next step would be learning how to Connect Spreadsheet to Google Forms

Related

Googlesheet editing permission of specific range to specific gmail id

I want to allow editing of specific range to specific gmail id. For example A1 contains gmail id of Tom and I want to allow Tom to edit B1 to G1, and A2 contains gmail id of James and I want to allow James to edit B2 to G2. How Should I do that. There are 400 different gmail id of different persons I want to allow them to edit specific ranges according to the gmail id present in column A. How should I do that. Manually doing this is very time consuming.
How to protect specific cells on a spreadsheet
You can use the following sample code that protects the cells from A1 to B10.
// Protect range A1:B10, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var protection = range.protect().setDescription('user name or ID just as a reference');
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
Since you will have the list of IDs on the same spreadsheet, you can modify the code and use a loop to grab the ID from each user and set the protected range and user according to your needs.
Something really important for this to work is that if you run this script you need to be the owner of the spreadsheet, otherwise you may get an error.
You will also need to have the users' email address along with the ID to set them as editors.

Import data from yahoo finance to Google sheets [duplicate]

This question already has answers here:
Scraping data to Google Sheets from a website that uses JavaScript
(2 answers)
Closed last month.
I want to import the data from options tab in yahoo finance to my google sheet. It is this tables:
Picture with the table I want to import
First of all, you can see a box with different dates that when you change the date the URL change. The difference between the URLS is that you need to sum to the previous number 604800 and then you get the correct URL.
Well if you use Excel, you can download the data (is in the table 3 the ones I want) without any issue, but you need to be changing the website manually every time that the date change.
So I was thinking to use the ImportXML or ImportHTML of google sheet. For example if you use in the main page: https://finance.yahoo.com/quote/VZ?p=VZ
This formula: =importXML("https://finance.yahoo.com/quote/VZ?p=VZ";"//*[#id='quote-header-info']/div[3]/div[1]/div/span[1]")
You will get the value of the stock in that moment but if you change the website url for the one of the options: =importXML("https://finance.yahoo.com/quote/VZ/options?date=1618531200&p=VZ";"//*[#id='quote-header-info']/div[3]/div[1]/div/span[1]")
You got a NA value... even if the value is there and the HTML code of the website is the same... and this for me does no make sense.
So I do not know how I should do to can download the data from the tab "options", and is frustrating cause it must be possible as it is really "simple" to get it in Excel.
Some suggestion?
I am not sure about that function but I have downloaded historical stock pricing on Yahoo using a script.
function importCSVFromWeb() {
// Provide the full URL of the CSV file.
var csvUrl = "https://query1.finance.yahoo.com/v7/finance/download/MSFT?period1=1577806579&period2=1609428979&interval=1d&events=history&includeAdjustedClose=true";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
Here is my sheet after running the script
Many thanks, it worked well. I slightly adapted code to work as a formula:
function importCSVFromWeb( url ) {
// Provide the full URL of the CSV file.
var csvUrl = url; // url example = "https://query1.finance.yahoo.com/v7/finance/download/%5EGSPC?period1=1495670400&period2=1653436800&interval=1wk&events=history&includeAdjustedClose=true"
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActiveSheet();
// sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
return csvData;
}

Google Sheets - Pushing a row to a second spreadsheet based on a value being entered

I'm new to this,
I have 2 google spreadsheets:
Spreadsheet A: The active sheet Containing multiple tabs with information to be Pushed to B.
Spreadsheet B: A spreadsheet with a single tab. The same headers and structure as spreadsheet A.
Based on the user selecting the answer "Yes" in the first column of any of the 1 tabs in Spreadsheet A, I would like that entire row to move over to Spreadsheet B.
I have modified a script that works on a single spreadsheet (ie moving rows from tab to tab) to attempt to get it to work between spreadsheets:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tss = SpreadsheetApp.openById('B').getSheetByName('Sheet 1');
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(r.getColumn() == 1 && r.getValue() == "Yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var target = tss.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
}
}
Probably needless to say, this yields no result. Having searched through a number of posts and forums I only see individuals posting about how to move rows between tabs but not between entirely separate spreadsheets. Is it even possible to do this? If so, what am I doing wrong in the script?
Thank you so much for anyone who takes the time to assist.
Anthony
Following a dialogue with the OP in the comments section, it was indicated that the original spreadsheet did not to be kept secret.
Consequently, the desired functionality can be provided by using a combination of IMPORTRANGE() and QUERY() in the spreadsheet with no need to use Google App Script. For instance,
=QUERY(IMPORTRANGE(url,range),"select A where B matches 'Yes'") or similar
This imports data from a second spreadsheet and then the QUERY() function acts as a way of filtering the imported range by certain criteria.
Once the imported range is authorised, the editors of the spreadsheet can access it by, e.g. removing or modifying the query. You could prevent this by protecting that particular cell, if needed.

How to use SUMIFS in an older version of Google Sheet - where SUMIFS was not implemented?

I have a large spreadsheet with I think 1000 formulas and I want to move them to an older version of the Google Sheet, because of the enhanced protection options I have there. (Basically I need to keep some sheets only for some people, while the rest to all with the link).
The tabels are not mine, so I don't know the logic of them - I just need to port them to the older version. Example of a formula is:
=SUMIFS('FC Eingabe'!$K$2:$K$142,'FC Eingabe'!$R$2:$R$142,"August",'FC Eingabe'!$S$2:$S$142,"100% | Auftrag erteilt")
Changing the SUMIFS to a solution from SUMIFS function in Google Spreadsheet is too time consuming to be worth it.
Is there any way I could add something like a custom function SUMIFS to that old Google Sheet? - something like a VBA piece of code, maybe. Or an add-on.
Please help!
Here is a quick and dirty (no error checking etc) attempt at a custom function:
function SUMIFS()
{
var test, result = 0;
var sumArray = arguments[0];
for (var i = 0, length = sumArray.length; i < length; i++)
{
test = true;
for (var j = 1, argLength = arguments.length; j < argLength; j+=2)
test = test && (arguments[j][i][0] == arguments[j+1]);
if (test) result += sumArray[i][0];
}
return result;
}
Alternatively, you could use a Find and replace (accessed with Ctrl+H) in the current Sheets version. It must be done in the current version as finding and replacing inside formulae is not supported in the old version.
This should work for two conditions, as in your OP:
Find: ^=SUMIFS(\(.+?,.+),(.+?,.+),(.+?\))$
Replace with: =SUM(FILTER$1=$2=$3)
and ensure "search using regular expressions" and "also search within formula" are checked.

How to protect from editing only rows that are odd numbered?

In Google Sheets how do I protect the rows in Column A which are odd from any kind of edits?
I would like to use this function
=ISODD(ROW(A1))
Protected sheets and ranges gives you this by default
Sheet1!A1
I can do this
Sheet1!A1:A1000
which will protect all 1000 rows but how do I use functions in that so I can only use ODD rows.
Here is a picture of that feature:
As mentioned in the comments on your question, I don't believe there is currently any way of manipulating range protection with Google Apps Script.
So the only option that I can think of is to manually apply the protection to 500 individual cells (in your example).
A workaround - that is not particularly tidy - is to use data validation to thwart (ultimately not prevent) entering data in even rows, with this sort of arrangement (accessed from Data, Validation...):
Savvy users who have access to the spreadsheet will be able to go in to data validation and circumvent this, though.
Google has created api for protecting sheets using google app script.
For example, to protect range A1:B10, You can run
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var protection = range.protect().setDescription('Sample protected range');
If You want to protect rows alternatively, You can try something like this
function alternateRowProtection() {
var totalRows = SpreadsheetApp.getActiveRange().getNumRows();
var sheet = SpreadsheetApp.getActiveSheet();
var row = 1;
while (row < totalRows)
{
if(row%2 == 0){
sheet.getRange(row).protect().setDescription('This is protected.');
}
row++;
}
}

Resources