Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

How do you track automated data inputs in Google Sheets over time?

I have a google sheet that runs a report on data that is automatically updated. What I would like to do is compare the new inputs with the old inputs to determine if the changes were positive or negative. How would you go about automating a sheet to track these changes?

  • Changes happen monthly
  • there would be a score 1-100; 100 being the best
  • would like to store this data over time for a historical view

Any advice would surely be appreciated

The numbers in each criteria change every month producing a score at the end of the table called Current Score

enter image description here

This score is then pulled into the historical tab as the "Current Score"

What I would like to see happen is that the Current score be saved every month and processed with a percentage change month over month

enter image description here

So I would need a function that stores a copy of the results before they change, processes a new score, and then calculates the difference between the two. Example here is the Dec score (stored values) compared to the most recent score.

Here is a link to the working example https://docs.google.com/spreadsheets/d/1ImbRhWqGjvIx2CFRKapZ2wmxC9qpSKxxCbHr5tPOBOs/edit#gid=0

question from:https://stackoverflow.com/questions/65937896/how-do-you-track-automated-data-inputs-in-google-sheets-over-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solution

You can automate this process by using Google Apps Script. Open the script editor by clicking on Tools > Script Editor. It is based on JavaScript and allows you to create, access and modify Google Sheets files with a service called Spreadsheet Service.

In addition, you can use Time-driven triggers to run the script automatically once a month. To set it up, click Triggers in the left bar, then Add Trigger and select Time-driven in Select event source. You can now specify the month timer and the exact day and hour you want the script to run. However, I recommend that you do some testing before setting up the trigger to check that you get the desired results. You can test the code by clicking Run in the Editor.

Explanation of the code

There are three functions in the code. The main function is called updateScores and it does what you described in the question. It takes the current score, stores it in a new column and calculates the difference from the last month. Try this function and if you like the result, you can put the trigger in the main function. This way, the trigger calls main which its only responsibility is to call the other two functions. The first is updateScores, which I have already explained, and the second is clearScores, which clears all the values of Reports so you don't have to do it manually and you can start writing the new values for the new month.

I have added some comments so you can understand what each line does.

var lr = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('report').getLastRow()

function updateScores() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Historical')
  var currentValues = ss.getRange('B2:B'+lr).getDisplayValues() // get current score
  ss.insertColumnsAfter(2,2) // insert two new columns (current score and percent difference)
  ss.getRange('D2:D'+lr).setValues(currentValues)  // paste stored score
  ss.getRange('C2:C'+lr).setFormula('=if(D2=0,"N/A",B2/D2-1)') // apply formula for last stored scores
  ss.getRange('E2:E'+lr).setFormula('=if(F2=0,"N/A",D2/F2-1)') // correct formula reference
  ss.getRange('E2:E'+lr).copyFormatToRange(ss,3,3,2,lr) // copy format percent
  ss.getRange('F2:F'+lr).copyFormatToRange(ss,4,4,2,lr) // copy format scores

  var month = new Date().toString().split(' ')[1] // get current month
  ss.getRange('D1').setValue(month + ' score') // write current month on last stored scores
  var diff = ss.getRange('E1').getDisplayValue() // get diff symbol
  ss.getRange('C1').setValue(diff) // write diff 

}

function clearScores(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('report')
  ss.getRange('B2:G'+lr).clear()
}

function main(){
  updateScores()
  clearScores()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...