简体   繁体   中英

(Google sheets script) How to add a value for the whole column at regular intervals

I'm new in this service useful for those like me that is not practical in the scripts of google. I'm looking for all-in-one google script for solve 2 problems:

1) I have in the following google sheet a table where every value in the "H" field, should be corrected adding the number 24 to each row, so as to have the value 42 instead of 19 in the column "H".

---example of table that I have now:
  id      hour              H   T   C   M   Q
  87      10/04/2018 15:11  18  19  430 19  38

  ---example of table that I want:
  id      hour              H   T   C   M   Q
  87      10/04/2018 15:11  42  19  430 19  38

2) My table refresh enterelly every 1 minute, so i need to re-add the number 24 to the "H" column every 1 minute.

Thanks to those who will take the time to help me!

You can use a function like this:

function add24toH(){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Foglio1');
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  for(var i=1;i<vA.length;i++){
    vA[i][2]+=24;//H
  }
  rg.setValues(vA);
}

You can lookup all of the methods here .

All my script are 3: with the first we import a .csv file inside the google sheets, with the 2nd one we sort the table, and with the 3rd one we add +24 to the "H" column. So now i post the script:

function importCSVFromGoogleDrive() {
var file = DriveApp.getFilesByName("*.csv").next();
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, 
csvData[0].length).setValues(csvData);
}

function AutoSortOnEdit() {
var sheetNames = ["Foglio1"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(function(name) {
var sheet = ss.getSheetByName(name);
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, 
sheet.getLastColumn());
range.sort({column: 1, ascending: false});
});
}

function add24toH(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Foglio1');
var rg=sh.getDataRange();
var vA=rg.getValues();
for(var i=1;i<vA.length;i++){
vA[i][2]+=24;//H
}
rg.setValues(vA);
}

Thanks!!

This code merge all script in oll-in-one

function onEdit() {
myFunction1(); // call function #1
myFunction2(); // call function #2
}

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