简体   繁体   中英

Need help in google sheet - script

I am trying to copy the logger values into a dedicated tab(history) in the same spreadsheet with a date and time stamp, I am getting values in the logger just need to arrange them and copy them into a dedicated tab(history). its gonna have 2 trigers per day and every day a new column in the new dedicated tab(history). thank.

 // custom menu function function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('History').addItem('save','saveData').addToUi(); } // function to save data function saveData() { // starts with active sheet for data entry var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Moshiko"); var sheet2 = ss.getSheetByName("History"); // collects values in data entry row var total = sheet.getRange("Moshiko:O11.O34");getValues(). var daily = sheet:getRange("Moshiko.R11;R34").getValues(): var portfolio = sheet.getRange("Moshiko;L11.L34"):getValues(). var lastportfolio = sheet;getRange("Moshiko.Q11:Q34").getValues(); var curentstock = sheet.getRange("Moshiko:K11.K33");getValues(). var laststock = sheet:getRange("Moshiko.P11,P33"),getValues(), var buystock = sheet,getRange("Moshiko,I11,I33"),getValues() var all = [[total;daily.portfolio;lastportfolio,curentstock,laststock,buystock,]]; Logger.log(all);

Issues:

  1. The return of .getValues() is 2 dimensional array. Appending [[]] will make it 4 dimensional array. .setValues() can only take 2 dimensional array.

  2. The data collected is in row data. Each row represent sub-array in our 2d array.

    Row data example: [[a][b][c][d]]

This is what it looks like in sheets:

在此处输入图像描述

Solution:

  1. Merge the collected row data using concat(). This will make [[a][b][c][d]] into [a,b,c,d] .
  2. Since the structure of the concatenated data is now in single row format, we can transpose it to make it column data.

Example Data:

例子

Code:

function myFunction() {
  // starts with active sheet for data entry
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Moshiko");
  var sheet2 = ss.getSheetByName("History");


  // collects values in data entry row
  var total = sheet.getRange("Moshiko!O11:O34").getValues();
  var daily = sheet.getRange("Moshiko!R11:R34").getValues();
  var portfolio = sheet.getRange("Moshiko!L11:L34").getValues();
  var lastportfolio = sheet.getRange("Moshiko!Q11:Q34").getValues();
  var curentstock = sheet.getRange("Moshiko!K11:K33").getValues();
  var laststock = sheet.getRange("Moshiko!P11:P33").getValues();  
  var buystock = sheet.getRange("Moshiko!I11:I33").getValues();

  var columns = [["Total", "Daily", "portfolio", "lastportfolio", "currentstock", "laststock", "buystock"]]
  var date = Utilities.formatDate(new Date(), "GMT+8", "dd/MM/yyyy")
  var dateStamp = [["Date stamp", date]]
  
  //concat each 2d array to make 1d array and enclose the 1d arrays with '[]'  to make it 2d. 
  var arr = [[].concat(...total),[].concat(...daily),[].concat(...portfolio),[].concat(...lastportfolio),[].concat(...curentstock),
            [].concat(...laststock),[].concat(...buystock)];

  //set dateStamp
  sheet2.getRange("A1:B1").setValues(dateStamp);
  //set columns
  sheet2.getRange("A2:G2").setValues(columns);
  //set values
  Logger.log(arr)

  var transposed_arr = transpose(arr);
  Logger.log(transposed_arr.length);
  Logger.log(transposed_arr[0].length);
  sheet2.getRange(3, 1, transposed_arr.length, transposed_arr[0].length).setValues(transposed_arr);
}

function transpose(matrix) {
  return matrix[0].map((col, i) => matrix.map(row => row[i]));
}

Output:

输出

Reference:

Array.concat

setValues

getValues

Ty so much, i have a question tho, in this lines i didnt understand what i need to fill. if i need to fill??

//concat each 2d array to make 1d array and enclose the 1d arrays with '[]' to make it 2d. var arr = [[].concat(...total),[].concat(...daily),[].concat(...portfolio),[].concat(...lastportfolio),[].concat(...curentstock), [].concat(...laststock),[].concat(...buystock)];

once again, thanks alot! you are a life saver

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