Is there an easier way to clear a Google sheet using script? - google-apps-script

I am trying to find a better way to clear filters on 12 different sheets of Google versus what I have put in. This clearing function takes the longest amount of time of my script to run.
An example of what is complete and works is below. Each sheet has 46 columns that I need to clear, and there are 12 sheets. This seems a little excessive but this is the only way that I've found that will do this, and it is a necessary step otherwise the other script won't run. Any way to improve on this? It's the same row on every sheet, exact same info, just different things could be filtered.
var IT = SpreadsheetApp.openById('1ktIHQMhr8RcoHJishstT4xDtO2PnU26MzDAW0Fe1Cks').getSheetByName('IT');
IT.getFilter().removeColumnFilterCriteria(1);
IT.getFilter().removeColumnFilterCriteria(2);
IT.getFilter().removeColumnFilterCriteria(3);
IT.getFilter().removeColumnFilterCriteria(4);
IT.getFilter().removeColumnFilterCriteria(5);
IT.getFilter().removeColumnFilterCriteria(6);
IT.getFilter().removeColumnFilterCriteria(7);
IT.getFilter().removeColumnFilterCriteria(8);
IT.getFilter().removeColumnFilterCriteria(9);
IT.getFilter().removeColumnFilterCriteria(10);
IT.getFilter().removeColumnFilterCriteria(11);
IT.getFilter().removeColumnFilterCriteria(12);
IT.getFilter().removeColumnFilterCriteria(13);
IT.getFilter().removeColumnFilterCriteria(14);
IT.getFilter().removeColumnFilterCriteria(15);
IT.getFilter().removeColumnFilterCriteria(16);

Related

Do you have to use libraries to share functions between sheet bound scripts?

I currently have two sheets, one of them runs some scripts (including one to import data from a third sheet) to generate a list and another pulls in the resulting data to create a visualization.
Is there any way for the first sheet to communicate the need for the second one to start and get back the output page? Triggering a remote function sounds weird, but they are both spreadsheets scripts that work in that context, manipulating the sheets not just data.
I don't think a library quite works because it creates multiple sheets at the target location but I only want one of them back.
Sheet A takes about 20 minutes to run and Sheet B takes about 2 minutes to run.
The ugly solution is to import all the code from B into A and do all the work there, and I will do that if necessary but I kind of prefer to have the visualization off on its own away from the data that is being worked with.
To clarify, right now the way this works is:
I change a variable in sheet A > I run sheet B, I have my visualization
What I need to do is to trigger this process from an external form so me working both parts manually needs to be cut from the system.

Script to clear a range but keep formulas - Gsheets

Specifically, I need a script to help me clear values from multiple rows but without clearing the formulas on those rows.
I have tried .clearContent which seems to just clear the whole row altogether without any option to keep the formulas.
I know that there might be some add-on that could help on a single sheet but I need to apply the procedure on multiple sheets so I would like to use a script for it.
Thanks!
Use range.getFormulas() to store your formulas, then call range.clearContent(), finally paste the formulas back with range.setFormulas().

Google sheets importData, importRange speed / interval - ways to improve it?

According to google documentation (https://support.google.com/docs/answer/58515?hl=en) current refresh/recalculate times are as follows;
To change how often some Google Sheets functions update:
Open a spreadsheet. Click File > Spreadsheet settings. Under
"Recalculation," choose a setting from the drop-down menu. Click Save
settings. Note: External data functions recalculate at the following
intervals:
ImportRange: 30 minutes ImportHtml, ImportFeed, ImportData, ImportXml:
1 hour GoogleFinance: 2 minutes
Is there any way in which we can speed this up? For example deleting the cell and then replacing it will force a refresh of the data. Maybe this could be done via a script for example. Can a script even be used on its own to pull in data from another sheet?
Does anyone have any ideas of how this could be done? Ideally I want a refresh of every 5 minutes.
Many thanks
I actually found a solution which seems to work quite well;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1").getRange('A1').setValue('');
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1").getRange('A1').setValue('=IMPORTDATA("https://www.dropbox.com/9329382983/file.csv?dl=1")');
Clear the cell with the importdata formula, 2. add the importdata formula back in.
You can then set this a script to run every x minutes.
from my understanding, everything recalculated when values are added or edited.
so maybe a script that will write a random value to a cell you aren't using (ie: Z500 or something)
then set a Time-driven triggers to trigger that script
MAYBE this'll work?

Set multiple cell values based on active cell

I've been looking around a lot and can't seem to find a solution to this problem, so here goes. Basically, I have a judging form where users will input a series 6 scores, along with a project ID. I will then access this data from a separate spreadsheet, and use it in my primary spreadsheet. I will want to average the data etc. but that is not important. What I can't figure out for the life of me, is how to write to multiple cells in my main spreadsheet based on where I write my script.
That sounded a little confusing so let me clarify. Lets say I name my function doThis. doThis will take in a range, such as AA:AB, and then operate on it, taking averages etc. etc. I will put in my main spreadsheet, lets say in cell A1 =doThis(exampleRange) (where exampleRange may just be an import statement from another spreadsheet). I then want the data from doThis to be written to as many cells as are necessary. Basically, lets say there are three projects that have been judged, and for each project I want doThis to output three metrics.
I then want the data from doThis to be written from A1:C3, but only have to write doThis(exampleRange) in cell A1. As I said, I've searched quite a bit on this, but I'm almost certainly missing something, so any assistance would be greatly appreciated. Of course, if you need any clarification I am more than happy to help.
EDIT FOR MORE INFO: Alright, let me clarify my use case a little bit. Basically, the form has 8 different sheets, each for a different category. On the form, once selecting a category the judge then selects scores in 6 categories, and then submit it. That will all be in a SEPARATE spreadsheet. In my main spreadsheet, where I will be doing the data analysis, I want to have 3 columns per category, so a total of 24 columns in total. So I want to type =doThis(respectiveRange) at A1,D1,G1... etc.
The actual action of the code will be the same between all three, various metric computation etc, which is all simple enough. I want to have doThis to be able to write to as many rows as in its respective area as needed based on the number of responses, and want it to work whether I write it it A1, D1 or whatever. So while the example code below is good, I want the A1:C3 part of it to be dynamically set based on where the =doThis(respectiveRange) is placed in the spreadsheet, and on how many responses are in the respectiveRange. I might be explaining this in a confusing way, but I am definitely more than happy to keep on clarifying as long as it takes!
You need to concatenate the data from multiple cells.
function moveCells() {
var sheetSource = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('ShheetName');
var twoDimensionalArrayOfSourceValues = sheetSource.getRange("A1:C3").getValues();
var sourceValuesAsString = twoDimensionalArrayOfSourceValues.toString();
sheetSource.getRange("A1").setValue(sourceValuesAsString);
};

ScriptDb query limit

Here is my issue. I have a spreadsheet with multiple sheets, and each sheet has about 300-500 rows. I am using ScriptDb to store the data for each sheet.
What I am currently doing is calling a custom function in 300-500 cells in each sheet to populate certain cells with data, and what happens is that some will populate and the rest will error out saying i've queried the database too many times in a short period. Obviously having to query the database for each cell isn't the best solution.
How would I go about querying all the data for the current sheet and then having that data available to grab for each cell. What I've read is that you can't really have "global" variables in GAS, but have to use things such as CacheService or ScriptDB, which is what i'm trying to do. I'm just querying it too much.
Is there some way to populate all the cells from 1 function call instead of 1 call for each cell? What am I missing or what other solutions are there?
Just realized a similar question was asked earlier today: Google Spreadsheet Script invoked too many times per second for this Google user account
Yes its possible. Simply return an array from your function. It will work like an arrayformula.
Of course your cells would need to be contiguous.

Resources