'Looking to automate weekly snapshots by recording specific rows
I am looking to create a script that will take the values from example (A1:C48) and record them to a different spread sheet at specific times in order to create automatic weekly generated reports for sales, finance, and employee efficiency. I found this code which records one cell but I need it to record a range of cells. I need to make sure it only records the specific numbers in the cells and not the formulas used to generate the cells. Below is a copy of the code that I have found that records one specific cell.
// Menu for testing your script
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Record value')
.addItem('Record now','recordValue')
.addToUi();
}
// Record history from a cell and append to next available row
function recordValue() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Example sheet - automatically updating");
var date = new Date();
var value =sheet.getRange("B1").getValue();
sheet.appendRow([date, value]);
}
I have found that duplicating the following code will let me record more than one cell at a time but not in the proper formatting that is required for this project
var date = new Date();
var value =sheet.getRange("B1").getValue();
sheet.appendRow([date, value]);
Any and all help would be greatly appreciated!!!
Current script edit:
// Menu for testing your script
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Record value')
.addItem('Record now','recordValue')
.addToUi();
}
function Timer() {
try {
var ss = SpreadsheetApp.openById("290415534"); // The id of the spreadsheet you want to get data from
var sh = ss.getSheetByName("Sheet1");
var data = sh.getRange("A1:C48").getValues();
sh = ss.getSheetName("Sheet2");
var date = new Date();
var last = sh.getLastRow();
sh.getRange(last+1,1).setValue(date); // Date in column A
sh.getRange(last+1,2,data.length,data[0].length).setValues(data);
// Put data starting column B
}
catch(err) {
Logger.log(err);
}
}
Solution 1:[1]
I really can't test this but I'm pretty sure it will work. You can assign this function to Time Driven Trigger.
Assuming you are using the New script editor put the function below into Code.gs:
function Timer() {
try {
var ss = SpreadsheetApp.openById("xxxxxxxxx"); // The id of the spreadsheet you want to get data from
var sh = ss.getSheetByName("Sheet1");
var data = sh.getRange("A1:C48").getValues();
sh = ss.getSheetName("Sheet2");
var date = new Date();
var last = sh.getLastRow();
sh.getRange(last+1,1).setValue(date); // Date in column A
sh.getRange(last+1,2,data.length,data[0].length).setValues(data); // Put data starting column B
}
catch(err) {
Logger.log(err);
}
}
Next on the left look for the alarm clock "Triggers". The Trigger dashboard will appear. Select "Add Trigger" and the Trigger dialog will appear. Choose which function should be set to "Timer" the name of my script. Select Event Source and Time-Driven. Select the Type and Interval. And now this function will be run automatically every interval.
Notice you have to use openById because there is no active spreadsheet. Insert the id of the spreadsheet containing the data. You can get the id from Google Drive -> Get link. I assume both source and destination are in the same spreadsheet file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TheWizEd |
