D
D
Dima2020-12-24 18:31:38
Google Apps Script
Dima, 2020-12-24 18:31:38

How to automatically save cell value in Google Sheets daily?

There is a sum of changing values, every evening I need to store the value of this sum, so that they can then compare them with the next day, and so on. How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2020-12-24
@qpz

The simplest thing is to create a script and connect a time trigger.
Add Code to Project to Table

/**
 *
 */
function createTrigger() {
  ScriptApp.getProjectTriggers().forEach(
    (trigger) =>
      trigger.getHandlerFunction() === 'saveData' &&
      trigger.getEventType() === ScriptApp.EventType.CLOCK &&
      (ScriptApp.deleteTrigger(trigger) ||
        console.info(`Tirgger ${trigger.getUniqueId()} was deleted`))
  );
  // every minutes for testing
  // ScriptApp.newTrigger('saveData').timeBased().everyMinutes(1).create();
  // at 9 o'clock every days
  ScriptApp.newTrigger('saveData').timeBased().atHour(9).everyDays(1).create();
}

/**
 *
 */
function saveData() {
  const book = SpreadsheetApp.openById(
    '1FUSSiDQoXyvKXfzYydoUUfcCGYq_TskpRiwfb28_1Z0'
  );
  const sheet = book.getSheetByName('Лист1');
  const value = sheet.getRange('A1').getValue();
  book.getSheetByName('Лист2').appendRow([new Date(), value]);
  console.info(`saveData was called successful`);
}

Don't forget to change the Table ID 1FUSSiDQoXyvKXfzYydoUUfcCGYq_TskpRiwfb28_1Z0to yours.
The table must contain two sheets: "Sheet1" and "Sheet2". From 9 am to 10 am, the program reads the value from the cell once Лист1!A1and adds a new row to Лист2.
Call the function createTriggeronce from the editor.
Example Spreadsheet with code https://docs.google.com/spreadsheets/d/1FUSSiDQoXy...
In the picture, I marked in red what needs to be clicked. Blue - needs to be changed before the first run.
5fe4bd91d80c7094551103.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question