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.
Related
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)
}
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]);
}}
}
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
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)
I am trying to figure out how to add a timestamp on a new row that is created in Google Sheets.
So whenever a new row is created, one of the cells in that row will have a timestamp reflecting when the row was created.
Is this possible?
Thanks!!
Add this function:
function addTimeStamp(e) {
var typeOfChange = e.changeType;
//Logger.log('typeOfChange: ' + typeOfChange);
//Logger.log('typeof typeOfChange: ' + typeof typeOfChange);
if (typeOfChange === "INSERT_ROW") {
var timeStamp = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
var activeRangeIs = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
//Logger.log(activeRangeIs);
var whatRow = activeRangeIs.getRow();
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(whatRow, 3).setValue(timeStamp);
};
};
Then use an installable trigger. Resources menu, Current Project Triggers.