[英]Need help in google sheet - script
我正在嘗試將記錄器值復制到具有日期和時間戳的同一個電子表格中的專用選項卡(歷史)中,我在記錄器中獲取值只需要排列它們並將它們復制到專用選項卡(歷史)中。 它每天將有 2 個觸發器,並且每天在新的專用選項卡(歷史)中都有一個新列。 感謝。
// 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);
.getValues()
的返回是二維數組。 附加[[]]
將使其成為 4 維數組。 .setValues()
只能采用二維數組。
收集的數據是行數據。 每行代表我們二維數組中的子數組。
行數據示例: [[a][b][c][d]]
這是它在工作表中的樣子:
[[a][b][c][d]]
變成[a,b,c,d]
。示例數據:
代碼:
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:
Ty 這么多,我有一個問題,在這行中我不明白我需要填寫什么。 如果我需要填寫?
//將每個二維數組連接成一維數組,並用“[]”將一維 arrays 括起來使其成為二維數組。 var arr = [[].concat(...total),[].concat(...daily),[].concat(...portfolio),[].concat(...lastportfolio),[] .concat(...curentstock), [].concat(...laststock),[].concat(...buystock)];
再次,非常感謝! 你是救生員
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.