I shared a file of Google sheets with my collaborator, and every day he will input the data into the row of Google Sheets, and I want to lock that row after 1 hour after he finishes his work!
You can use Apps Script for this. Use a time-driven installable trigger in a script that protects/locks the sheet.
Your code should be something like this:
var sheet = SpreadsheetApp.getActiveSheet();
var permissions = sheet.getSheetProtection();
permissions.setProtected(true);
sheet.setSheetProtection(permissions);
Related
I am building a google sheet spanning multiple sheets. I have an index page with hyperlinks to few select sheets for convenience.
I'm looking to pin this column to show up on all the sheets.
Most suggestions I found online is about copying over the column from index page to all sheets, which I find hard to maintain and scale.
Is there a better way to achieve this?
Suggestion:
Perhaps you can add this sample bound script below to your spreadsheet file then save & run it from the editor:
To create a bound script in Google Sheets, open your spreadsheet and click Extensions or Tools > Apps Script or Script Editor
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var indexPage = ss.getSheetByName("Sheet").getRange("A:A");
var sheets = ss.getSheets();
sheets.forEach(cursheet => {
indexPage.copyTo(ss.getSheetByName(cursheet.getName()).getRange("A:A")); //Copies index on every sheets' column A
ss.getSheetByName(cursheet.getName()).setFrozenColumns(1); //// Freezes the first column on every sheet
});
}
This script will also automatically run every time you edit (that means every time you press Enter key after editing/adding a new cell value) your spreadsheet file as it was configured with onEdit trigger
Sample Result
After saving & running the script from the Apps Script editor:
Sheet:
Wish List
Laptops
Apparel
Reference:
Google Apps Script
Apps Script: setFrozenColumns(columns)
I have a spreadsheet that contains four different sheets
Day1 | Day2 | Day3 | Summary
in the summary sheet i will be referencing a cell in either Day1,Day2,Day3 sheets
for example i'm using this formula in the summary sheet to get a value from Day3 sheet
='Day3'!$D2
so far its working however i want the sheet name (in this case the 'Day3' in the formula above) to be static.
meaning if i rename the Day3 sheet into something else for example rename it to Day4, and create another sheet named Day3,
i want the formula above to still reference ='Day3'!$D2
because as of now if i do the steps i mentioned above, ='Day3'!$D2 automatically changes to ='Day4'!$D2 when i rename the Day3 sheet
is this possible in google sheets?
This is an example of recalculation in Google Sheets, and for your use case, there is currently no way of disabling this feature.
As a workaround, you can use Google Apps Script to create an installable trigger to change the sheet reference manually everytime the sheet structure changes.
Sample Code:
function createTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("retainReference")
.forSpreadsheet(ss)
.onChange()
.create();
}
function retainReference() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Summary");
var range = sheet.getRange('A1');
range.setFormula("='Day3'!D2");
}
Note: You only need to run createTrigger() function manually.
Sample Output:
When renaming sheet Day3 to sheet Day4:
Reference:
Installable Triggers
Very simple question.
How do you make endless row on spreadsheet? Like Excel.
I have this problem when I use google sheet to scan barcodes.
When rows reached 1000, I need to add more manually.
But sometimes I forget, then I keep scanning.
After that I check my sheet, I missed a lot of input but I don't remember which barcode was the last one, so I have to do them all over again after increasing the rows.
If google sheets has the infinite rows like Excel, I won't have to worry about it no more.
Do you guys have any solutions on this?
Use Apps Script to Make Your Sheet Dynamic
With Apps Script, you can write a function to detect how many cells are between the data inserted and the end of the spreadsheet, and add rows if they are too close.
function addRowsIfCloseToEnd() {
let file = SpreadsheetApp.getActive();
// INPUT YOUR SHEET NAME HERE
let sheet = file.getSheetByName("Sheet1");
let maxRow = sheet.getMaxRows()
let lastRow = sheet.getLastRow()
// In this example, when the values are 100 rows
// from the end of the sheet, it will add 100 rows
// to the end. Change this to your liking.
if (maxRow - lastRow < 100) {
sheet.insertRowsAfter(maxRow, 100)
}
}
In this example, the function checks when values are less than 100 rows from the end of the spreadsheet, and if it is, it will add 100 extra rows to the sheet.
You should adjust these numbers to suit your workflow, I don't know how many bar codes you scan or how quickly.
You have two options for how to trigger this:
onEdit
This is a simple trigger designed to run a function every single time there is an edit on the sheet. You can call the previous function like this:
function onEdit() {
addRowsIfCloseToEnd()
}
If you have authorized your script, then this should run every time you make an edit:
In this example I only add 10 rows every time, to demonstrate.
Time-based trigger
Depending on how many barcodes you scan and how quickly, you may not want this function to run every single time you scan a barcode, in which case you can make a trigger to run every 5 minutes for example:
function createClockTrigger() {
ScriptApp.newTrigger("addRowsIfCloseToEnd")
.timeBased()
.everyMinutes(5)
.create();
}
References
Apps Script
Tutorials
Simple Triggers
onEdit
ClockTriggerBuilder
I saw several complains about the delay of updating data through IMPORTRANGE in Google Sheets but I need the opposite and don't want the second sheet to get updated automatically, just update at the end of the day for example.
The code is already like this:
=IMPORTRANGE("The Key","The_Page!B:D")
I hacked around this by, on both spreadsheets by creating a refresh loop using a NOW cell, with both spreadsheets crossreferencing each other's NOW cell.
When the original sheet gets appended with a form submission or something, it updates its own NOW cell, and reupdates own IMPORTRANGE cell. The second spreadsheet follows suit, updating its own NOW cell to provide the original sheet with the correct data. Because the second spreadsheet has updated itself, it also updates the main IMPORTRANGE which refreshes the data you want it to display in the first place, as well as the IMPORTRANGE cell which gets the NOW cell from the original spreadsheet
At least, I'm pretty sure that's how it works. All I know, and all I care about, frankly, is that it works
Maybe you need to use the script editor and write a simple function of the kind:
function importData()
{
var ss = SpreadsheetApp.getActiveSpreadsheet(); //source ss
var sheet = ss.getSheetByName("The_Page"); //opens the sheet with your source data
var values = sheet.getRange("B:D").getValues(); //gets needed values
var ts = SpreadsheetApp.openById("The Key"); //target ss - paste your key
ts.getSheetByName("Name of the target sheet").getRange("B:D").setValues(values);
}
And then add a time-driven trigger to this project (Resources > Current project's triggers > Add a new one).
Is it possible to insert the date/time when a row is inserted into a google spreadsheet.
Please note this is different from the NOW() method, which will insert the current date time as your view the spreadsheet...
The main purpose of this is to turn the google spreadsheet into a 'google form' which can be sent out to collect feedback and will have a history of when responses were given
something like the below in a Google App Script? This should set the value of the a set column at the time the edit is made.
If you are unfamiliar with GAS, select "Tools"->"Script Editor" from Spreadsheet menu. Select "Blank Project", then wipe out what's there and replace with this. Save it (name is unimportant) and go back to your Spreadsheet to try an edit and see results.
function onEdit() {
var dateColNum = 6 //column F
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
//set date in same row as edit happens, at fixed column
ss1.getActiveSheet().getRange(ss1.getActiveRange().getLastRow(), dateColNum, 1, 1).setValue(new Date())
}