我对Google App脚本有疑问
下面是我尝试过的代码...
@Tanaike帮助了我以下代码,但是现在的问题是放置3个维度
function SpreadbyStoreName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
// remove header row values from array
values.shift();
// data structure
var ds = {};
values.forEach(function(row) {
var storename= row[0]; // 1st col
var fruits = row[1]; // 2nd col
var notformatteddate = row[2]; // 3rd col
var startdate = Utilities.formatDate(new Date(notformatteddate), "GMT+1", "MM/dd/yyyy")
ds[storename] = ds[storename] || {}; // vendor
ds[storename][fruits] = ds[storename][fruits] || {};
ds[storename][fruits][startdate] = ds[storename][fruits][startdate] || [];
ds[storename][fruits][startdate] = [parseInt(row[3]), parseInt(row[4])];
});
var rs = {};
Object.keys(ds).forEach(function(s) {
rs[s] = rs[s] || [];
Object.keys(ds[s]).forEach(function(f) {
rs[s][f] = rs[s][f] || [];
Object.keys(ds[s]).forEach(function(sd) {
if(ds[s][f][sd]) {
rs[s][f].push([sd, ds[s][f][sd][0], ds[s][f][sd][1]]);
}else{
rs[s][f].push([s, 0, 0]);
}
})
})
})
Logger.log(rs)
Object.keys(ds).forEach(function(v) {
var headers = [[s, 'Fruits'].join(' '), 'NumberSold', 'UniqueSold'];
ds[s].unshift(headers);
try {
ss.insertSheet(headers[0]);
} catch (e) {}
var sh = ss.getSheetByName(headers[0]);
sh.clear();
sh.getRange(1, 1, ds[s].length, 3).setValues(ds[s]);
})
}
基本上,以上代码的目的是获取[商店名称,水果,创建日期,销售数量,唯一销售],然后按“商店名称”列将数据拆分到Google表格中的单独标签中。
使用上面的代码,我无法获得可以使用的正确对象格式
https://docs.google.com/spreadsheets/d/171S9c079hHBgqgsouZPb09k220HEFTF13y2bB187-N8/edit?usp=sharing
上面是示例数据的链接
答案 0 :(得分:0)
按商店名称创建和加载工作表
function sheetByStoreName() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Sheet148');
var values = sheet.getDataRange().getValues();
var header=values[0];
values.splice(0,1);
//Loading the object
var ds = {eA:[]};//for store names and object names
values.forEach(function(row) {
if(!ds.hasOwnProperty(row[0])) {
ds[row[0]]=[]; //new store
ds[row[0]].push({fruit:row[1],datecreated:Utilities.formatDate(new Date(row[2]), "GMT+1", "MM/dd/yyyy"),numbersold:row[3],uniquesold:row[4]});
ds.eA.push(row[0]);
}else{
ds[row[0]].push({fruit:row[1],datecreated:Utilities.formatDate(new Date(row[2]), "GMT+1", "MM/dd/yyyy"),numbersold:row[3],uniquesold:row[4]});
}
});
//Looking at the output
var html="<style>td,th{border:1px solid black;}</style><table>";
for(var i=0;i<ds.eA.length;i++) {
for(var j=0;j<ds[ds.eA[i]].length;j++) {
Logger.log('--%s,%s,%s,%s,%s',ds.eA[i],ds[ds.eA[i]][j].fruit,ds[ds.eA[i]][j].datecreated,ds[ds.eA[i]][j].numbersold,ds[ds.eA[i]][j].uniquesold);
html+=Utilities.formatString("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",ds.eA[i],ds[ds.eA[i]][j].fruit,ds[ds.eA[i]][j].datecreated,ds[ds.eA[i]][j].numbersold,ds[ds.eA[i]][j].uniquesold);
}
}
html+='</table>';
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Example');
//Loading the sheets
for(var i=0;i<ds.eA.length;i++) {
var sh=ss.getSheetByName(ds.eA[i]);
if(!sh) {
var sh=ss.insertSheet(ds.eA[i]);
}
sh.clearContents();//clear each stores sheet
sh.appendRow(header);//put the header on first
for(var j=0;j<ds[ds.eA[i]].length;j++) {
sh.appendRow([ds.eA[i],ds[ds.eA[i]][j].fruit,ds[ds.eA[i]][j].datecreated,ds[ds.eA[i]][j].numbersold,ds[ds.eA[i]][j].uniquesold]);
}
}
}