I'm currently using the Google Spreadsheets API V4 to read a spreadsheet. It works fine, I read and write through it and the changes are immediately reflected in the Google spreadsheet. However, I want changes I make through the web browser to be reflected in my app as well. I can't see any events in the API, and I can't find any way to do it in Google either.
The spreadsheet is relatively large (1500+ rows), so I can't constantly check every single cell for a change.
You can set up an onEdit trigger that will log the edit date and other details in another sheet inside the same spreadsheet.
function onEdit(e){
var range = e.range;
range.setNote('Last modified: ' + new Date());
}
Related
I am looking for a way to import the data from one google drive sheet to another using ImportRange formula. However, I want the data to be synced once per day at a certain time instead of automatically updating as the formulas seems to do. Any help would really be appreciated
Formula used:
={IMPORTRANGE(B2,"sheet1!$A$1");IMPORTRANGE(B3,"sheet1!$A$1");IMPORTRANGE(B4,"sheet1!$A$1"); IMPORTRANGE(B5,"sheet1!$A$1");
IMPORTRANGE(B6,"sheet1!$A$1")
}
You can create a Script (Google Apps Script) that copies the data automatically with Time-driven Triggers (https://developers.google.com/apps-script/guides/triggers).
function copyData() {
// Gets data
var data = SpreadsheetApp.openById("ID1").getSheetByName("SheetName").getRange("A1:B2").getValues();
// Copies data
SpreadsheetApp.openById("ID2").getSheetByName("SheetName").getRange("A1:B2").setValues(data);
}
I have created a Google Sheet add-on that includes a bunch of Google Apps Scripts plus a bunch of formatting over a number of sheets (tabs). When I publish this to the G Suite Marketplace, will that formatting of the sheets (tabs) be included? For example, I have entered column headers, styled the text (colour, font etc). The formatting and text instructs the user where to enter the needed data that allows the add-on to function.
I have searched through the documentation and other Stackoverflow questions, but have not found a response.
The published Google Sheets add-ons doesn't include a copy of the spreadsheet. To include them you could make that your add-on creates a copy of the spreadsheet that includes the formatted sheets or you could publish that spreadsheet as a template.
Reference
Create a file from template
Look at the Create a template with an add-on section
I have found an answer. #Rubén was likely referring to this solution.
To accomplish this, follow these steps.
Create your spreadsheet with associated formatting, text, colours etc.
Go into Share settings and share the spreadsheet with the option Anyone who has the link can view
Then grab the ID of the spreadsheet within the URL (ex. https://docs.google.com/spreadsheets/d/ID_IS_HERE/edit#gid=0)
To copy that spreadsheet use the following code in your add-on.
function importSheet() {
var ss = SpreadsheetApp.openById("sheet ID goes here");
var sheetToCopy = ss.getSheets()[0]; //gets the first sheet
var destination = SpreadsheetApp.getActiveSpreadsheet();
sheetToCopy.copyTo(destination);
}
If your source spreadsheet has multiple sheets, you can get the number of sheets using var number = ss.getSheets().length; then loop through the above code using number to copy each individual sheet.
Regarding the source spreadsheet, Google provides the following detail
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
I hope this helps someone else.
I receive the data on the P/E multiple in Google Sheets via GOOGLEFINANCE in the following manner:
=GOOGLEFINANCE("GOOGL","pe")
How to write script in Google Sheets that would record the P/E values (or better the values of a particular cell) every day as at, say, 21:00?
Use Google Apps Script.
You want to write a function that uses the Spreadsheet API to get the value of a given Range in a given Sheet, and then append that value in a new row on a different sheet.
You also want to set up a time-based trigger to execute that function.
As an example, for a bound script with only one worksheet:
function copyA1toA2onSameSheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange("A2").setValue(
sheet.getRange("A1").getValue()
);
}
Note that getActiveSheet will always return the first sheet from a time-based trigger, as there is no UI instance. If you need a different sheet, consider getSheetByName, or using getSheets().
I currently use =ImportRange to bring in some data from a public sheet I do not own.
However, it is a lot of data and importrange tries to refresh the data on every page access, and doesn't cache it locally.
Is there a script I can run instead to get the data via importrange, then hard code it into my own sheet and only update it once a week?
You can use the Google Sheets add-on called sheetgo, to automatically update your reference from another sheet. You can use 30 updates for free every month or pay for more updates
But I found this to be effective.
function refreshSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name);
//Your operations on the sheet
Utilities.sleep(1000);
}
while(true){
Utilities.sleep(604800000);
refreshSheet();}
Context:
I have a Google Sheets document with my daily tasks in it. Many of my tasks involve me checking to see if changes have been made to other Google Sheets (referenced in my daily task document) in the past day.
Goal:
I want a function which will tell me when one of the referenced Google sheets was last altered.
Here is an example:
https://docs.google.com/spreadsheets/d/1JV-ZvO1P5PPU2Eoi7qSIKaeBWgHkmAA0BLEh8OmLCGI/edit?usp=sharing
I want the function to be in the B column.
This is fairly simple but will take a bit of code. You can go to Tools> Script Editor and create a function like such:
function timestamp() {
return new Date()
}
You can then use that function in an If statement like the following:
IF(A2="","",timestamp(A2))
If the cell is blank the function will return blank, but when the cell is updated it will timestamp the change in that cell.
If you are unfamiliar with Apps Script you can learn how it works here.