繁体   English   中英

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

[英]script to copy values only from one google sheet to another is not working

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

我尝试了各种不同的方法,但都没有奏效。

特别是代码块是这样的:

//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);
}

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

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");

但是您没有对“第一”做任何事情,所以我想知道您是否想获得模板的副本?

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

我认为使用这样的引用有点危险: source.getSheets()[15]; 因为我相信它们可以通过用户移动标签来改变。

我没有看到你在哪里删除了标签/工作表。

暂无
暂无

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

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