简体   繁体   English

是否可以将 append 水平放置到谷歌表格中的下一列?

[英]Is it possible to append to the next column horizontally in google sheets?

I currently have this script.我目前有这个脚本。 I works fine, however, I would prefer if it appended to the next column horizontally each time a choose to append.我工作得很好,但是,如果每次选择 append 时,我希望它水平附加到下一列 Right now it appends after the last row.现在它附加在最后一行之后。 Please be very specific about what needs changing.请非常具体地说明需要更改的内容。 Thanks!谢谢!

function myFunction() {
  var ss = SpreadsheetApp.openById("1aACg8M89LLxILIvUqWT2ZnrykF8e3BkIiamj2NhKrmc")
  var sheet = ss.getSheetByName("Framsida");
  var data = sheet.getRange(2, 3, 25, 1).getValues();
  for (var i = 0; i < data.length; i++) {
    Logger.log(data[i][0]);
    Logger.log(data[i][1]);
  }
  var row= sheet.getLastRow();
 Logger.log(row);
 Logger.log(data);
   var temp = data
     for (i in temp){
       var write = temp[i]
       if (row >=10) {ss.getSheetByName("Archive").appendRow(write)};

     } 
   ss.getSheetByName("Framsida").getRange(2, 3, 25, 1).clear();

}

You want to retrieve the values from the cells of "C2:C26" on the sheet "Framsida", and want to put the values to the next column of the last column on the sheet "Archive".您想从工作表“Framsida”上的“C2:C26”单元格中检索值,并希望将值放在工作表“存档”上最后一列的下一列。

In order to retrieve the last column of the sheet, please use getLastColumn() .为了检索工作表的最后一列,请使用getLastColumn() Here, I would like to propose 2 patterns for achieving your goal.在这里,我想提出 2 种模式来实现您的目标。

Pattern 1:模式一:

In this pattern, getValues and setValues are used.在此模式中,使用getValuessetValues

function myFunction() {
  var ss = SpreadsheetApp.openById("1aACg8M89LLxILIvUqWT2ZnrykF8e3BkIiamj2NhKrmc")
  var srcSheet = ss.getSheetByName("Framsida"); // <--- Modified
  var srcRange = srcSheet.getRange(2, 3, 25, 1);
  var data = srcRange.getValues();
  
  var dstSheet = ss.getSheetByName("Archive");
  dstSheet.getRange(2, dstSheet.getLastColumn() + 1, data.length, 1).setValues(data);
  
  srcRange.clear();
}

Pattern 2:模式二:

In this pattern, copyTo is used.在此模式中,使用了copyTo

function myFunction() {
  var ss = SpreadsheetApp.openById("1aACg8M89LLxILIvUqWT2ZnrykF8e3BkIiamj2NhKrmc")
  var srcSheet = ss.getSheetByName("Framsida"); // <--- Modified
  var srcRange = srcSheet.getRange(2, 3, 25, 1);
  
  var dstSheet = ss.getSheetByName("Archive");
  srcRange.copyTo(dstSheet.getRange(2, dstSheet.getLastColumn() + 1), SpreadsheetApp.CopyPasteType.PASTE_VALUES);
  
  srcRange.clear();
}
  • When srcRange.copyTo(dstSheet.getRange(2, dstSheet.getLastColumn() + 1), SpreadsheetApp.CopyPasteType.PASTE_VALUES);srcRange.copyTo(dstSheet.getRange(2, dstSheet.getLastColumn() + 1), SpreadsheetApp.CopyPasteType.PASTE_VALUES); is modified to srcRange.copyTo(dstSheet.getRange(2, dstSheet.getLastColumn() + 1), SpreadsheetApp.CopyPasteType.PASTE_VALUES, true);修改为srcRange.copyTo(dstSheet.getRange(2, dstSheet.getLastColumn() + 1), SpreadsheetApp.CopyPasteType.PASTE_VALUES, true); , the values are put by transposing. , 这些值是通过转置放置的。
    • If appended to the next column horizontally means that you want to horizontally put the values from "C2:C26" to the last column by transposing, please test this.如果appended to the next column horizontally意味着您想通过转置将“C2:C26”中的值水平放置到最后一列,请测试一下。

References:参考:

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

相关问题 Google表格脚本-在数组中搜索并在下一列中替换值 - Google Sheets Script - Search in Arrays and Replace Values in Next Column 水平打印Google表格中的图表 - Print a chart from Google Sheets horizontally 如何使用谷歌应用脚本将数组写入工作表列并将自定义公式添加到旁边的列? - How to write array to sheets column and add custom formula to column next to it using google apps script? 如何获取Google Sheets API追加响应? - How to get google sheets api append response? Google 工作表 api append 多个带格式的值 - Google sheets api append multiple values with formatting 如何遍历范围,获取特定单词并将其粘贴到相邻列,直到使用脚本在 Google 表格中找到下一个关键字? - How to iterate over a range, get a specific word and paste it to an adjacent column until next key word is found in Google Sheets using scripts? 在引导程序 5 中单击 Prev/Next 时水平滚动列 - Scroll column-wise horizontally on clicking Prev/Next in bootstrap 5 是否可以使用JavaScript将数据写入Google表格? - Is it possible to write data to google sheets using JavaScript? 如何获取脚本以在 Google 表格中以正确的顺序附加数据? - How to get a script to append the data in the right order in Google Sheets? Append Google Apps 脚本中来自表格数据的新幻灯片 - Append New Slides from Sheets Data in Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM