【问题标题】:Value Paste Google Sheets to new Spreadsheet值将 Google 表格粘贴到新的电子表格
【发布时间】:2014-12-13 17:47:29
【问题描述】:

从我下面的尝试中您可能会意识到,谷歌脚本并不是我最大的优势。

我们所有的学校奖励/制裁目前都进入谷歌表格。在每个学期结束时,我想将其中一些表格存档到新的工作簿/电子表格中。发生这种情况时,他们只需要重视副本。

我可以让这段代码适用于任何单独的工作表,但我对 for 循环不是很熟悉,因此无法同时为所有四张工作表实现这一点。
感谢各位专家提供的任何帮助!
`

> function copySheetValues()
{
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = ['tally', 'commendations', 'detentions', 'omegas'];
  for (var i=0, i<3, i++)
    var sheet = source.getSheetByName(sheetname[i]);
    sheet.activate();
  var sourcename = sheet.getSheetName();
  var sourceDataRange = sheet.getDataRange();
  var sourceSheetValues = sourceDataRange.getValues();
  var sourceRows = sourceDataRange.getNumRows();
    var sourceColumns = sourceDataRange.getNumColumns();
  var ssNew = SpreadsheetApp.create('Archive');
  var url = (ssNew.getUrl());
  var destination = SpreadsheetApp.openByUrl(url);
  

  destination.insertSheet(sourcename, 0);
  destination.getDataRange().offset(0, 0, sourceRows, sourceColumns).setValues(sourceSheetValues);
}

`

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    使用不同的方法,您能看看这是否适合您吗?

    function backup() {
    
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var bs = SpreadsheetApp.openById(ss.copy('Archive').getId());
    var sheets = ['tally', 'commendations', 'detentions', 'omegas'];
    SpreadsheetApp.flush();
    var bsl = bs.getSheets().forEach( function (s) {
    if(sheets.indexOf(s.getName()) !== -1) {
    s.getDataRange().copyTo(s.getDataRange(), {
                contentsOnly: true
            });
    } else {
    bs.setActiveSheet(s);
    bs.deleteActiveSheet();
    }
    });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多