简体   繁体   English

Google App Script - 将数据插入下一列

[英]Google App Script - Insert Data Into Next Column

This script fetches the data from the sheet and insert it into a new sheet.此脚本从工作表中获取数据并将其插入到新工作表中。 I want the second function to insert into the third row each time.我希望第二个函数每次都插入第三行。

I want the result to be -我希望结果是——

Timestamp |时间戳 | Impressions |印象 | CTR点击率

function AggregateData() {
Impressions();
CTR();
}

function Impressions() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Metrics");
var source = sheet.getRange("G16:H16");
var values = source.getValues();
var sheet = ss.getSheetByName("Aggregate");
values[0][0] = new Date();
sheet.appendRow(values[0]);
};

function CTR() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Metrics");
var source = sheet.getRange("F16:G16");
var values = source.getValues();
var sheet = ss.getSheetByName("Aggregate");
values[0][0] = new Date();
sheet.appendRow(values[0]);
};

This code appends the second function in the second column itself below the result of the first function.此代码将第二个函数附加到第一个函数的结果下方的第二列中。

This code will copy your data to a new sheet in the order you want.此代码将按照您想要的顺序将您的数据复制到新工作表中。 However, I have reduced it to one function.但是,我已将其简化为一种功能。 The reason your code puts the new data in a new row is this line:您的代码将新数据放在新行中的原因是这一行:

Sheet.appendRow(data)

puts the data you supply to a new row which is empty.将您提供的数据放入空的新行。 That is also the reason why i have combined the two functions into one.这也是我将这两个功能合二为一的原因。

function AggregateData() {
Impressions();
}

function Impressions() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Metrics");
 var source = sheet.getRange("F16:H16");
 var values = source.getValues();
 var pasteValues = []  // create new array which will hold the data you need to be pasted into the aggregate Sheet
 pasteValues[0] = [] // Adding a second dimesion to the array
 var sheet = ss.getSheetByName("Aggregate");
 // below 3 lines does the switch of data and adds the time stamp
 pasteValues[0][0] = new Date();
 pasteValues[0][1] = values[0][2]   //Copy the col h (impressions value) value into the new array
 pasteValues[0][2] = values[0][1]   //Copy the col G (impressions value) value into the new array
 sheet.appendRow(pasteValues[0]); //Append the new array into the sheet
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM