简体   繁体   中英

How do I automatically archive a google sheet with a google script?

I have a google sheet that is scraping data from a table from an html homepage.

The data is in a simple google sheet with A1:H1 containing the headers/labels and A2:H2 containing the values

Every time that data changes, every time my google sheet is updated with new data, I want the new data from A2:H2 to be "archived"/copied automatically to another google (sub)sheet.

Apparently, I can do this with a Google Script (that is then run automatically every day with a trigger) - I tried to use this tutorial here: https://www.computerworld.com/article/2469616/business-intelligence/business-intelligence-79661-how-to-create-an-automatically-updating-spreadsheet.html#slide7

The code they have/give is:

function storeData(){
var datarange = sheet.getDataRange();
var numRows = datarange.getNumRows();
var numColumns = datarange.getNumColumns();
var nextColumn = numColumns + 1;
sheet.getRange(1,nextColumn).setValue(new Date());
  for (var i=2; i <= numRows; i++){
var numLikes = sheet.getRange(i, 3).getValue();
sheet.getRange(i, numColumns + 1).setValue(numLikes);
}
}

When I run this I get the following error: "sheet" is not defined

So, do I need to put in the data range I want to copy between ()? Like this?

function storeData(){
var datarange = sheet.getDataRange(A2:H2);
var numRows = datarange.getNumRows();
var numColumns = datarange.getNumColumns();
var nextColumn = numColumns + 1;
sheet.getRange(1,nextColumn).setValue(new Date());
  for (var i=2; i <= numRows; i++){
var numLikes = sheet.getRange(i, 3).getValue();
sheet.getRange(i, numColumns + 1).setValue(numLikes);
}
}

When I run this I get the following error: missing ")" sign in line 2

Can somebody help?

You need to define sheet.

function storeData(){

  var ss = SpreadsheetApp.openById('ID'); // Select Spreadsheet by ID
  var sheet = ss.getSheetByName('Name'); // Select your Sheet by its Name
  var datarange = sheet.getDataRange();
  var numRows = datarange.getNumRows();
  var numColumns = datarange.getNumColumns();
  var nextColumn = numColumns + 1;

  sheet.getRange(1,nextColumn).setValue(new Date());
  for (var i=2; i <= numRows; i++){
var numLikes = sheet.getRange(i, 3).getValue();
sheet.getRange(i, numColumns + 1).setValue(numLikes);
}
}

The first error occurs because sheet isn't defined. There are several ways to define it. If the sheet to be archived will be always the same, use

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Sheet1'); // Replace Sheet1 by the actual name of the sheet to be archived

The second error occurs because getDataRange should not include any argument. Instead of

var datarange = sheet.getDataRange(A2:H2);

use

var datarange = sheet.getRange('A2:H2');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM