List in sheet doesn't update list in form - google-sheets

I'm a beginner at this so apologies. I'm trying to have a sheet with data in column A be the options in a specific list in a form. Ultimately I would like many columns of data be the reference of specific lists. The form doesn't need to sync with the list. I'd prefer to do that by running script in sheet.
This is how far I am but it's only pulling cell A1.
Right now the form is simple and only has one drop down question.
Many thanks for any help.
var FORM_ID = '1Edo3yPmfPLQXPRff6z8oDsYW0Hp-Mm5SYLISPnyTXfo';
var LIST_SHEET_NAME = 'List';
var range = 'A:A';
var ITEM_ID = '983918997';
function updateListInForm() {
var values_ = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(LIST_SHEET_NAME)
.getRange(range)
.getValues();
values_[0][0] = values_[0][0].toString();
for (var i = 1; i < values_.lenght; i++) {
values_[0].push(values_[i][0].toString())
}
var form = FormApp.openById(FORM_ID);
var list = form.getItemById(ITEM_ID);
list.asListItem().setChoiceValues(values_[0]);
function getitemsofform() {
var form = FormApp.openById(FORM_ID);
var items = form.getItems(FormApp.ItemType.LIST);
for (var i in items) {
Logger.log('id: ' + items[i].getId() + ' title: ' + items[i].getTitle());
}
}
}
One more thing. Can anyone point me to a program or app that teaches google script? I would like to start from scratch. ie. this is what var means, this is how to write a simple function

Related

Google Sheets Function trying to rename a sheet

The following function is designed to get my spreadsheet, add a new tab, insert a new tab with a query that runs, and then save the new tab under a new name.
Everything works except the new tab always is inserted as then next unused tab number and not renamed. Tried a number of variations (setName, etc.) and nothing seems to work.
Help!
function costIncome() {
var spreadsheetId =https://docs.google.com/spreadsheets/d/thisIsTheSheetID/edit';
var targetRange = 'PlaySheet!B:F';
var SELECT = 'select B,E,F,sum(C) ';
var WHERE = ' ';
var GROUPBY = ' group by B,F,E order by D,B';
var HAVING = ' ';
var SQL = SELECT + WHERE + GROUPBY + HAVING;
//var SQL = 'select B,E,F,sum(C) group by B,F,E';
var Query = '=QUERY('+targetRange+',\"'+SQL+'\")';
var currentDoc = SpreadsheetApp.openByUrl(spreadsheetId);
currentDoc.insertSheet('costfunds');
var pushQuery = currentDoc.getRange(1, 1).setFormula(Query);
var pullResult = currentDoc.getDataRange().getValues();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Get Active spreadsheet object
var sh = spreadsheet.getActiveSheet(); // Get Active sheet's object
sh.setName("Name changed!")
}
I found an answer elsewhere
SpreadsheetApp.flush();
tempSheet.setName("MYCHANGEDSHEET");
I lost the link for the original post, but thanks to him/her/them.
So many things are poorly functioning/documented in G-Sheets. SMH.

Script on google sheet for filtering a specific name

need some help with script on google sheets
I'm having some issues to try to use "setVisibleValues" and I got an error saying "Exception: Visible values are not currently supported. As an alternative specify a list of hidden values that excludes the values that should be visible.". But my problem is, I've a huge number of employee names on a list, and I wanna filter just based in one person.
I cant show some information, but the thing is: "I just wanna filter one value, and I don't know how to do it using "setHiddenValues".
function Filtersheet() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A3:S').activate();
spreadsheet.getRange('H3').activate();
var criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues([''])
var criteria = SpreadsheetApp.newFilterCriteria()
.setVisibleValues(['tayzer'])
.build();
spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(8, criteria);
}
Lets say that the names of the Employees are : jorge, lucas, nuno, fernando, marta, beatriz, tayzer, larissa, and I wanna use the filter script to only show on the column the information related to tayzer
But my way to change the filter employee will be on a cell outside the script (cause I've around 200 employees)
Thanks in advance for the help
try this
function setFilter() {
// replace 'sheet name' with you sheet
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet name');
var range = ss.getDataRange();
var filter = range.getFilter() || range.createFilter()
var criteria = SpreadsheetApp.newFilterCriteria().whenTextContains('tayzer');
filter.setColumnFilterCriteria(8, criteria);
}
if i am not wrong if you put "jorge" in cell_C4 you want it to filter "jorge" in column E, if so try below code.
function setFilter() {
// replace 'sheet name' with you sheet
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet name');
var range = ss.getDataRange();
var filter = range.getFilter() || range.createFilter()
var criteria = SpreadsheetApp.newFilterCriteria().whenTextContains(ss.getRange('C4').getValue()).build();
filter.setColumnFilterCriteria(8,criteria)
}

Script for "Split text to columns" with form in Google Sheets

I am using IFTTT to push forms sent to my email address to a Google Sheet. The contents get pasted into column A in one big clump, and I can't figure out how to split the text into columns in a way that formats well. Ideally, I'd like to only include the answers to questions on the form in the columns to the right of column A.
Here is a sample of what gets pasted into column A:
New customer message on May 9, 2017 at 12:15 PM
You received a new message from your Online Store's contact form.
Name:
Jon Snow
Email:
sample#gmail.com
Q1:
Yes
Q2:
No
Q3:
yes
Q4:
no
Q5:
no
Is there some sort of script I could use to display the name, email address, and answers to the 5 questions in the 7 columns to the right of column A?
I have a rudimentary solution that you can build on if you like.
My solution assumes a few things, firstly the name is always going to be two words, secondly the answers are always going to be yes or no.
This will work for any number of rows in column A.
The whole solution is based on splitting up the string in a particular way, and it's not very fluid as it assumes the amount of spaces and the formatting will always be the same. It's a start :)
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var dataRangelength = ss.getDataRange().getLastRow();
var strRef = ss.getRange("A1:A" + dataRangelength);
var str = strRef.getValues();
for (var i = 0; i < dataRangelength; i++) {
var row = str[i];
var A1ref = i + 1
var rowRef = ss.getRange("A" + A1ref);
if (row != "") {
var arr = row.toString().split(" ");
var arr1 = arr[20].toString().split("\n");
var arr2 = arr[21].toString().split("\n");
rowRef.offset(0, 1).setValue(arr1[4] + " " + arr2[0]);
rowRef.offset(0, 2).setValue(arr2[4]);
rowRef.offset(0, 3).setValue(arr2[8]);
rowRef.offset(0, 4).setValue(arr2[12]);
rowRef.offset(0, 5).setValue(arr2[16]);
rowRef.offset(0, 6).setValue(arr2[20]);
rowRef.offset(0, 7).setValue(arr2[24]);
}}
}

Google script to send conditional emails based on cells in google sheets

I have a google sheet that I would like to have generate an email alert when one column is greater than the other. Specifically, column F > column G. Here is what I have so far, any advice would be greatly appreciated, as I do not have much skill writing functions.
function readCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Watch list");
var value = sheet.getRange("F2").getValue();
var value1 = sheet.getRange("G2").getValue();
if(value>value1) MailApp.sendEmail('example#gmail.com', 'subject', 'message');
};
Currently this only attempts to compare cell F2 to cell G2. Is there a way to make the function compare the entire F column against column G, and generate an email for each individual case where Fx > Gx ?
Thank you!!
You have to loop all over the range.
first instead of getting the content of one cell you'll need to get the content of all the column:
var value = sheet.getRange("F2").getValue();
become that
var values = sheet.getRange("F2:F").getValues();
(same for value1)
then you need to create an empty table that will collect the results:
var results = [];
and now you need to loop throught all the values:
for(var i=0;i<values.length;i++){
//do the comparaison and store result if greater for example
}
then you may send the result.
all put together it give something like that:
function readCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Watch list");
var values = sheet.getRange("F2:F").getValues();
var value1s = sheet.getRange("G2:G").getValues();
var results = [];
for(var i=0;i<values.length;i++){
if(values[i]<value1s[i]){
results.push("alert on line: "+(i+2)); // +2 because the loop start at zero and first line is the second one (F2)
}
}
MailApp.sendEmail('example#gmail.com', 'subject', results.join("\n"));
};
If you want to trigger that function automatically you'll also need to change the way you call the spreadsheet (instead of getActive.... you'll need to use openById)

Google Spreadsheet: Dynamic Hyperlink Formula

I'm trying to create a dynamic HYPERLINK formula that will automatically create the link based on the sheet name in Column A, but I'm not sure how (or if it's possible) to get the URL of the sheet based on the name.
Here's the setup:
Single Google Spreadsheet with multiple tabs
Tab names: 1500, 1501, 1502, Consolidated
On the Consolidated tab, I have two columns: Column A is the Sheet Name, and Column B is a HYPERLINK formula, that when clicked should open the corresponding sheet.
Is there a way to programmatically get the URL for the sheet based on the sheet name in Column A? Perhaps I could use a script to populate Column C with the URL, then use the following formula: =HYPERLINK(C2,A2)?
Thanks for the help!
Surprised to see this as a top search on Google but with no answer.
Anyway, here's the method I found that works for me: combine Hyperlink with values from a different column using the &, a basic example is shown below:
If you are trying to automatically generate direct URL's to specific sheets based on their name, and do not want to use scripts, you are out of luck. Currently, the only way to link directly to specific sheets is by appending the correct gid number to the spreadsheet URL. A gid must be either copied manually from the active sheet's URL, or automatically extracted with a custom function, created using scripts.
Since you're open to using scripts, it looks like i found a detailed tutorial of how to do this. https://www.benlcollins.com/spreadsheets/index-sheet/
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Index Menu')
.addItem('Create Index', 'createIndex')
.addItem('Update Index', 'updateIndex')
.addToUi();
}
// function to create the index
function createIndex() {
// Get all the different sheet IDs
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var namesArray = sheetNamesIds(sheets);
var indexSheetNames = namesArray[0];
var indexSheetIds = namesArray[1];
// check if sheet called sheet called already exists
// if no index sheet exists, create one
if (ss.getSheetByName('index') == null) {
var indexSheet = ss.insertSheet('Index',0);
}
// if sheet called index does exist, prompt user for a different name or option to
cancel
else {
var indexNewName = Browser.inputBox('The name Index is already being used,
please choose a different name:', 'Please choose another name',
Browser.Buttons.OK_CANCEL);
if (indexNewName != 'cancel') {
var indexSheet = ss.insertSheet(indexNewName,0);
}
else {
Browser.msgBox('No index sheet created');
}
}
// add sheet title, sheet names and hyperlink formulas
if (indexSheet) {
printIndex(indexSheet,indexSheetNames,indexSheetIds);
}
}
// function to update the index, assumes index is the first sheet in the workbook
function updateIndex() {
// Get all the different sheet IDs
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var indexSheet = sheets[0];
var namesArray = sheetNamesIds(sheets);
var indexSheetNames = namesArray[0];
var indexSheetIds = namesArray[1];
printIndex(indexSheet,indexSheetNames,indexSheetIds);
}
// function to print out the index
function printIndex(sheet,names,formulas) {
sheet.clearContents();
sheet.getRange(1,1).setValue('Workbook Index').setFontWeight('bold');
sheet.getRange(3,1,names.length,1).setValues(names);
sheet.getRange(3,2,formulas.length,1).setFormulas(formulas);
}
// function to create array of sheet names and sheet ids
function sheetNamesIds(sheets) {
var indexSheetNames = [];
var indexSheetIds = [];
// create array of sheet names and sheet gids
sheets.forEach(function(sheet){
indexSheetNames.push([sheet.getSheetName()]);
indexSheetIds.push(['=hyperlink("#gid='
+ sheet.getSheetId()
+ '","'
+ sheet.getSheetName()
+ '")']);
});
return [indexSheetNames, indexSheetIds];
}

Resources