仅将值从一个Google工作表复制到另一个工作表的脚本不起作用

时间:2019-01-11 18:38:02

标签: google-apps-script

我正在尝试仅将值从google工作表中的特定标签复制到另一个google工作表,然后删除该标签。基本上,我想将一些数据从一张纸存档到下一张纸。需要删除工作表,以便在下周将同一工作表#复制到存档工作表中。

我尝试了各种不同的方法,但没有一个起作用。

特别的代码块是这样:

//Archive the sheet from two weeks ago. This part is still being worked on. Once this is figured out, we can have it automate.
//Something in the final copyTo function is broken.
    function copyInfo() {
    var source = SpreadsheetApp.openById('1PDlQc3bHR4Wa2j72K01HiWCtrto36AXDhVf1d9Ajtpg');
    var sheet = source.getSheets()[15];

    var destination = SpreadsheetApp.openById('1MGN3UtOgVg2N6V4cWGqCIpFIITZZQV0OtAJNRevM09o');
    var valuesonly = SpreadsheetApp.CopyPasteType.PASTE_VALUES;
    sheet.copyTo(destination, valuesonly, true);
}

但是,这是完整的代码块,因此您可以看到我要执行的操作:

// popup menu on open
function onOpen() {
    Browser.msgBox('Black tabs = data transferred but need more data. Changes: Adhesive vs nonadhesive tabs were removed and in their place, Solvent and Latex tabs were created to follow the flow of the inventory template sheet.  IMPORTANT: Please notify Carrie if you add lines to the sheets so she can make sure data is correctly allocated on the weekly inventory template. Thank you!', Browser.Buttons.OK);
}

//Custom menu located on the top menu bar called "Custom Menu"
function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Custom Menu').addItem('Copy Weekly Inventory', 'copyTo').addSeparator().addItem('Archive', 'copyInfo').addToUi();
}

//Copy the 3rd sheet from the left to a new sheet, and name that sheet with today's date. 
//Copy the previous week's unopened data to new sheet.
function copyTo(spreadsheet) {
    var source = SpreadsheetApp.openById('1PDlQc3bHR4Wa2j72K01HiWCtrto36AXDhVf1d9Ajtpg');
    var sheet = source.getSheets()[2];
    var destination = SpreadsheetApp.openById('1PDlQc3bHR4Wa2j72K01HiWCtrto36AXDhVf1d9Ajtpg');

    sheet.copyTo(destination);

    var thisDate = new Date();
    Logger.log(thisDate);
    var month = new Array();
    month[0] = "Jan";
    month[1] = "Feb";
    month[2] = "Mar";
    month[3] = "Apr";
    month[4] = "May";
    month[5] = "Jun";
    month[6] = "Jul";
    month[7] = "Aug";
    month[8] = "Sep";
    month[9] = "Oct";
    month[10] = "Nov";
    month[11] = "Dec";
    var mo = month[thisDate.getMonth()];
    Logger.log(mo);

    var day = thisDate.getDate();
    Logger.log(day);

    var thisDatePart = mo + ' ' + day;
    Logger.log(thisDatePart);

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var first = SpreadsheetApp.setActiveSheet(ss.getSheetByName("Copy of WeeklyInventoryTemplate"));
    var second = ss.getSheetByName("Copy of WeeklyInventoryTemplate");
    second.setName(thisDatePart);

    var copyInfo = SpreadsheetApp.getActiveSpreadsheet().getSheets()[16];
    var source = copyInfo.getRange(6, 2, 315, 1)
    var destination = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(thisDatePart).getRange(6, 5, 315, 1)
    source.copyTo(destination);
}

//Archive the sheet from two weeks ago. This part is still being worked on. Once this is figured out, we can have it automate.
//Something in the final copyTo function is broken.
function copyInfo() {
    var source = SpreadsheetApp.openById('1PDlQc3bHR4Wa2j72K01HiWCtrto36AXDhVf1d9Ajtpg');
    var sheet = source.getSheets()[15];

    var destination = SpreadsheetApp.openById('1MGN3UtOgVg2N6V4cWGqCIpFIITZZQV0OtAJNRevM09o');
    var valuesonly = SpreadsheetApp.CopyPasteType.PASTE_VALUES;
    sheet.copyTo(destination, valuesonly, true);
}

1 个答案:

答案 0 :(得分:0)

这基本上就是您的功能归结为:

function copyTo() {
  var ss = SpreadsheetApp.getActive();
  var source1 = SpreadsheetApp.openById('1PDlQc3bHR4Wa2j72K01HiWCtrto36AXDhVf1d9Ajtpg');//same id
  var sheet1 = source1.getSheets()[2];
  var dest1 = SpreadsheetApp.openById('1PDlQc3bHR4Wa2j72K01HiWCtrto36AXDhVf1d9Ajtpg');//same id
  sheet1.copyTo(dest1);//So this is a copy of third sheet from left

  var thisDatePart =Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MMM dd")
  var second = ss.getSheetByName("Copy of WeeklyInventoryTemplate");
  second.setName(thisDatePart);   
  var source2 = ss.getSheets()[16].getRange(6,2,315,1);
  var dest2 = ss.getSheetByName(thisDatePart).getRange(6,5,315,1)
  source2.copyTo(dest2);//copy data from 17th sheet from left to sheet named month day i.e. theDatePart
}

function copyInfo() {
  var source = SpreadsheetApp.openById('1PDlQc3bHR4Wa2j72K01HiWCtrto36AXDhVf1d9Ajtpg');//different ids
  var sheet = source.getSheets()[15];//16th sheet from left
  var destination = SpreadsheetApp.openById('1MGN3UtOgVg2N6V4cWGqCIpFIITZZQV0OtAJNRevM09o');//different ids
  var valuesonly = SpreadsheetApp.CopyPasteType.PASTE_VALUES;
  sheet.copyTo(destination,valuesonly,true);//This copies the 16th sheet from left to another spreadsheet. 
}

您在这里有两行:

  var first = SpreadsheetApp.setActiveSheet(ss.getSheetByName("Copy of WeeklyInventoryTemplate"));
  var second = ss.getSheetByName("Copy of WeeklyInventoryTemplate");

但是您没有对“ first”做任何事情,所以我想知道您是否正在尝试获取模板的副本?

您还有一个名为“ spreadsheet”的函数参数,但从未在函数主体中使用它。

我认为使用像这样的引用是很危险的:source.getSheets()[15];,因为我相信可以通过用户移动选项卡来更改它们。

我没有看到您删除标签/工作表的位置。