复制行内容,但仅复制特定单元格,并添加时间戳。

huangapple go评论69阅读模式
英文:

Copy Row Contents but Only Certain Cells and add Timestamp

问题

以下是您要翻译的内容:

function onEdit(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Main');
  var date  = new Date();
  var rowIdx = sheet.getActiveRange().getRowIndex();
  var rowValues = sheet.getRange(rowIdx,1,1,sheet.getLastRow()).getValues();
  Logger.log(rowValues);
  var destValues = [];
  destValues.push(rowValues[0][0]);// copy data from col A to col A
  destValues.push(rowValues[0][2]);// copy data from col C to col B
  destValues.push(rowValues[0][1]);// copy data from col B to col C
  destValues.push(rowValues[0][3]);// copy data from col D to col D

  var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
  dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
}

请注意,我已经删除了代码部分以进行翻译,只返回了已翻译的部分。

英文:

I would like to have it that when a row is edited that it only copies the name phone and adds a timestamp of the edit on another tab in Google Sheets.

function onEdit(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Main');
  var date  = new Date();
  var rowIdx = sheet.getActiveRange().getRowIndex();
  var rowValues = sheet.getRange(rowIdx,1,1,sheet.getLastRow()).getValues();
  Logger.log(rowValues);
  var destValues = [];
  destValues.push(rowValues[0][0]);// copy data from col A to col A
  destValues.push(rowValues[0][2]);// copy data from col C to col B
  destValues.push(rowValues[0][1]);// copy data from col B to col C
  destValues.push(rowValues[0][3]);// copy data from col D to col D

  var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
  dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);



}

The above is working but i cannot figure out how to get the timestamp to show.

The outcome should display the roadname, phone and then C:C should have timestamp of change made.

I have updated the below sheet to show expected outcome

https://docs.google.com/spreadsheets/d/1V3Ne9L0kB97OkFKwlidx40yN__sx78aRik_Ujiej2Wc/edit#gid=1523701088

How I want it to look.
Result Image

Data source
Source Data

答案1

得分: 0

你的书面解释与示例输出不一致,而示例输出与你的脚本也不一致。在destValues.push()中,你将4个值推送到数组中(包括"Phone Number"、"Date Contacted"和"Clan Name"),但你的期望结果只有三列(不包括"Date Contacted"和"Phone Number"),但"Phone Number"实际上在"Clan Name"列中。

替换后的代码:

var date  = new Date();
destValues.push(rowValues[0][0]);// 将数据从A列复制到A列
destValues.push(rowValues[0][2]);// 将数据从C列复制到B列
destValues.push(date);// 将日期数据复制到C列
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);

希望这有所帮助。

英文:

Your written explanation is inconsistent with your sample output which is inconsistent with your script. destValues.push()-you are pushing 4 (four) values onto the array (including "Phone Number", "Date Contacted" and "Clan Name") but your expected outcome only has three columns (excluding "Date Contacted" and "Phone number"), but the "Phone number" is in the "Clan Name" column.


REPLACE

  destValues.push(rowValues[0][0]);// copy data from col A to col A
  destValues.push(rowValues[0][2]);// copy data from col C to col B
  destValues.push(rowValues[0][1]);// copy data from col B to col C
  destValues.push(rowValues[0][3]);// copy data from col D to col D
  var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
  dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);

WITH

var date  = new Date()
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(date);// copy data from col B to col C
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);

huangapple
  • 本文由 发表于 2023年2月16日 11:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定