I'm trying to find an Employee ID from the attendance which spans multiple sheets and pull the timestamp into a single sheet.
The Google sheet has multiple sheets in it. There are individual sheets for every working day:
Each attendance sheet has two columns. In order to know all the times of the employee's login I want to pull in all the occurrences of the employee ID from all the sheets and along with its timestamp to the Consolidation sheet: .
I've done a similar thing in Excel and guess can be done in Google sheet using Google Apps script.
It would be helpful if someone can guide me to a built-in or custom function in google sheets.
I'll help you with a basic outline, some advice, and some resources to help, but don't expect me to write the whole thing.
Step 1 - Create a custom menu option
You'll want to be able to access you script from the spreadsheet. You can accomplish this by creating a custom menu option. Here's an article by google on custom menus.
Although google uses the simple trigger onOpen, I've found installable triggers to be more reliable.
Step 2 - Get user input
It would be nice to be prompted for the id of the employee and have the script work it's magic. Here is an article by google on dialogs and sidebars that discusses how to get user input for a script.
Step 3 - Read and write data from the spreadsheet
You can access spreadsheeet data with the SpreadsheetApp. When calling your script from a custom menu, you can use SpreadsheetApp.getActiveSpreadsheet; however, if you want to test your code in the script editor, there is no "active spreadsheet", so use SpreadsheetApp.openById instead.
With access to the spreadsheet, you can call:
spreadsheet.getSheetByName("name").getRange(1, 1, 2, 2).getValues();
spreadsheet.getSheetByName("name").getRange(1, 1, 2, 2).setValues([[1, 2], [3, 4]]);
Note that getValues and setValues work with 2-dimensional arrays
WARNING - As usual, I/O takes a lot of processing time so avoid superfluously calling getRange().XetValues, this google article about appscript best practices goes into more detail. Also, if you have a LOT of attendance sheets, then you may want to consider condensing them into one.
Step 4 - Get the appropriate sheets
You'll need someway to distinguish which sheets in the spreadsheet are attendance sheets:
function isAttendanceSheet(sheet) {
return /Magical regex/.test(sheet.getName);
}
var sheets = spreadsheet.getSheets().filter(isAttendanceSheet);
Come up with the magical regex that works for you, and this will filter them.
Hope this helps and good luck!
Related
I have a Google Sheet that uses an IMPORTRANGE query to combine data from multiple other sheets. This combined import sheet is read by Google AppSheet. We have realized that the data AppSheet is reading is always outdated. It only reads the data as of the last time the sheet was manually opened.
I followed the steps in this post to try to fix this issue by creating this function: function refresh() {SpreadsheetApp.flush()}. I then set up a timed trigger to activate it once an hour. Logs show the function is running, but the data is still not updating until I manually open the sheet.
This is my first time using Apps Script. Any tips/ideas? Is there a different or better way to have the formulas update without opening the file?
Thank you for reading.
SpreadsheetApp.flush() only works for the script execution that calls it. If you need to refresh the data results from a formula it's uncertain how exactly the spreadsheet will respond as most of the formula calculations are done on the client side. You could verify this by yourself by using your web browser developer tools.
Anyway, spreadsheet formulas have several caveats so it will not be extrange that at some point you will have to rethink your solution. Assuming that you want to keep using AppSheet:
Use AppSheet for your front end and some no-code / low-code automation. Keep your app small, if you need many forms / views consider to distribute them among several apps.
Use Google Sheets only for data storage for your AppSheet app. Please bear in mind that it has 10 million cells limit for the whole spreadsheet, so you might want to delete the unused sheets and delete the unused columns and rows on each sheet.
You might use Google Apps Script to do the data import and transformation tasks. If you need that something be updated based on actions done on the AppSheet app, you might use an installable change trigger or use webhook from the AppSheet side to and a "simple" web application using Google Apps Script (you could use GET / POST http requests to trigger some Google Apps Script functions).
Also you might use other programming platforms for the data import / transformation tasks and keep using Google Sheets as your AppSheet database by using the Google Sheets API or other automation tools like Zappier, IFTTT, Integromat among many others.
solution #1
You can try this solution :
define a checkbox (for instance in A1 in tab Sheet1)
set this script
function myFunction() {
var chk = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange('A1')
chk.setValue(false);
SpreadsheetApp.flush();
Utilities.sleep(500);
chk.setValue(true);
}
define a trigger on it
define the formula as follows
=if(A1,importrange("1n-rjSYb63Z2jySS3-M0BQ78vu8DTPOjG-SZM4i8IxXI","A:Z"),"")
when A1 is unchecked, the result will be empty, then check A1 to fill once again the result as expected
solution #2
by script, try for instance
function myFunction() {
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet9')
var data = SpreadsheetApp.openById('1n-rjSYb63Z2jySS3-M0BQ78vu8DTPOjG-SZM4i8IxXI').getSheets()[0].getDataRange().getValues()
sh.getRange(1,1,data.length,data[0].length).setValues(data)
}
put a daily triger as needed
I'm looking a way to "conditionally protect" some of my sheet's ranges. I know it's not possible without a script... So I would need this:
On open, protect range (A6:A) from being modified if A2="C". Else if, leave unprotected.
This should be repeated on open in 10 sheets with given names (Com1, Com2,...,Com10)
Any help would be appreciated
Check out the Apps Script reference to access and modify protected ranges in sheets. You can build a Protection object which contains permissions for who can edit a given range.
Google Sheets has simple triggers including a function that will run onOpen() which you can put your code inside. The full Apps Script reference for extending Sheets provides a great resource for building functions to edit and manipulate structures and data of Spreadsheets.
You'll want to use .getRange('A6:A') and build a condition which calls the .protect() method if A2 = 'C'.
You can also you use SpreadsheetApp.getActiveSpreadsheet.getSheets() to get all the sheets from the spreadsheet and iterate through the returned array using forEach() to apply your protection to all sheets.
I want to write a Google Apps Script to generate a list of the IDs of the scripts bound to certain spreadsheets.
I cannot find a way to programmatically, via GAS, get the ID of a bound script.
If I have a spreadsheet object, I have methods like getId to get the ID of the spreadsheet, but not the bounded script.
Using the DriveApp, I can find standalone scripts using DriveApp.getFilesByType('application/vnd.google-apps.script') but not bound ones!
Any suggestions?
Unfortunately (by my own research) this is not currently possible. I have been wanting to programatically identify and access bound scripts for several years, I look into it periodically, I've always come up empty handed.
Haven't found any Google documentation explicitly on the topic, still hoping this hole gets filled eventually.
I am creating a spreadsheet portfolio. I came across some limitation, e. g. that I can not automate the process of importing the data from a website for different stocks. This is since the Index for the stock information on the website is often different from another stock. However there is the pattern that it is the next Index from a defined string e. g. "Branche". This made me wonder if I can automate the process with the Google Apps Script.
I wrote down the steps at first in Google Sheets. Then I formulated the steps in the Google Apps Script. Now I am stuck.
Step 1
=IMPORTXML("https://www.comdirect.de/inf/aktien/detail/uebersicht.html?ID_NOTATION=9386126";"//tr/td[#class='simple-table__cell']")
Step 2
=IMPORTXML(CONCATENATE("https://www.comdirect.de/inf/aktien/detail/uebersicht.html?ID_NOTATION=";"9386126");"//tr/td[#class='simple-table__cell']")
Step 3
=INDEX(IMPORTXML(CONCATENATE("https://www.comdirect.de/inf/aktien/detail/uebersicht.html?ID_NOTATION=";"9386126");"//tr/td[#class='simple-table__cell']");62;1)
Step 4 final product - just an idea not working yet
function import_branche() {
var url1 = "https://www.comdirect.de/inf/aktien/detail/uebersicht.html?ID_NOTATION="
var ulr2
var ticker = "//tr/td[#class='simple-table__cell']"
Index = find the INDEX with the String == "Branche"
return Index(IMPORTXML(CONCATENATE(url1;url2); ticker);(Index+1);1)
}
Ideally, I would like to have a function where I only need insert the link of the website and get the result. Here is the index for the information automatically found.
Google Apps Script can't execute Google Sheets spreadsheet functions like IMPORTXML, so you have two basic alternatives
Use Google Apps Script to get the result of a IMPORTXML formula from the spreadsheet, then use JavaScript to do the rest of the job
Do the job completely using Google Apps Script and JavaScript
Related
Google docs ImportXML called from script
(javascript / google scripts) How to get the title of a page encoded with iso-8859-1 so that the title will display correctly in my utf-8 website?
I have a spreadsheet with different sheets in Google sheet, 3 users can edit each one a sheet (protections are set, each user can edit only one sheet). They all can execute a google script function that writes what they edited in a summary sheet. I don't want anyone to be abble to edit the summary sheet, so I set myself as the only available editor.
So my problem is to authorize the 3 users, only through the google script function, to write in the summary sheet. I tried to use the following function :
var unprotected = summarySheet.getRange('G3:G10');
protection.setUnprotectedRanges([unprotected]);
but since the users are not allowed to edit the summary sheet, and since the function is run with the active user, so they can't give themselves the right to unprotect a range in the summary sheet... Do you know how to workaround this problem?
Thanks a lot!
I see two script-based choices, one easy and one quite hard, and one sheet-based choice, that is easiest:
Easy:
You run the "summarize" script instead of them or, you set the summarize script run on a trigger out of your account. Then you actually leave protections alone. You could set the summarize script to run on open with error catching if the user doesn't have the necessary authority to unprotect the summary sheet and/or write to the summary sheet.
Hard:
When they run the "summarize" script it calls a published standalone script that has been given the authorization to make the necessary protection changes. I'll be honest, I wouldn't be able to code this but have seen/heard of similar implementations.
Easiest:
Finally, I want to make sure you've considered having the summary sheet itself contain the necessary formulas, parsing, etc. to summarize data from the other sheets without any need of scripts for this aspect of the sheet. The sheet could call custom functions as needed if the parsing or other summarization functionality is beyond built-in functions' capabilities. The sheet could stay fully protected and update itself in real time as users enter data (no need for users to trigger the summary creation, unless spreadsheet settings have auto-recalculate turned off).
Edited to add: put in A1 of Summary sheet something like:
=summarize()
And have that custom function return a 2-dimensional array of the summarized data.