简体   繁体   English

如何更正将数据从一个 Google 电子表格导出到另一个的格式问题?

[英]How To Correct Format Issue Exporting Data From One Google Spreadsheet To Another?

Script Source: Coutesy of AJ Aviles脚本来源: Cutesy of AJ Aviles
Export Range of Data from One Google Spreadsheet to Another using Google Apps Script 使用 Google Apps 脚本将数据范围从一个 Google 电子表格导出到另一个

Issue: When exporting a range of data from one Google Spreadsheet to another Google Spreadsheet, missing data occurs because of an existing hyplink column.问题:将一系列数据从一个 Google 电子表格导出到另一个 Google 电子表格时,由于现有的超链接列而导致数据丢失。

Goal: Need assistance refining the app script to only pull the values and not bring over the formula that exist.目标:需要帮助改进应用程序脚本以仅提取值而不引入现有公式。

Sample Sheet (Sent To Archive) 样品表(发送至存档)

Sample Sheet (Recieve Data) 样品表(接收数据)

 function archive() { const sss=SpreadsheetApp.openById('1vOS00pJd2YdQNC9KEFuGTm8tSO7Ez1M9SI41-nrwlwU');//src const ssh=sss.getSheetByName("Send Data"); const tss=SpreadsheetApp.openById('1jNqtZB628GV8Y6fwvkdxTh7GVvQOwqvLdZRVZHIp_CQ');//tgt const tsh=tss.getSheetByName("Archive Here") const nsh=ssh.copyTo(tss); nsh.getRange("A2:Q50").copyTo(tsh.getRange("A1")); tss.deleteSheet(nsh); // This handles the Send Data sheet tab of the Parent sheet var sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID); var sourceWorksheet = sourceSpreadsheet.getSheetByName(sourceWorksheetName); var sourceData = sourceWorksheet.getRange("A2:Q50").activate(); // Gets data starting from A2 to Row Q and going down 30 // This handles the Archive Here from the sheet tab in Parent Sheet var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID); var targetWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName); var targetRange = targetWorksheet.getRange(1, 1, sourceData.getNumRows(), sourceData.getNumColumns()); var rowStart = 2; var columnStart = 1; var backgroundColor = ""; var kitSelectRowStarts = 2; var kitSelectRowEnds = 15; sourceNumberOfColumns = 15; // 30 Columns holding the Order Information sourceNumberOfRows = 50; // 55 Rows holding the Order Information for(var i = 0; i <= 30; i++){ for(var j = 0; j <= 52; j++){ backgroundColor = sourceWorksheet.getRange(rowStart + i, columnStart + j).getBackground(); formula = sourceWorksheet.getRange(rowStart + i, columnStart + j).getFormula(); text = sourceWorksheet.getRange(rowStart + i, columnStart + j).getValue(); fontWeight = sourceWorksheet.getRange(rowStart + i, columnStart + j).getFontWeight(); fontSize = sourceWorksheet.getRange(rowStart + i, columnStart + j).getFontSize(); fontColor = sourceWorksheet.getRange(rowStart + i, columnStart + j).getFontColor(); textHorAlignment = sourceWorksheet.getRange(rowStart + i, columnStart + j).getHorizontalAlignment(); textVerAlignment = sourceWorksheet.getRange(rowStart + i, columnStart + j).getVerticalAlignment(); //Logger.log(wrap[0][0]); // Override and set background, font weight, wrap, etc. // targetWorksheet.getRange(rowStart + i, columnStart + j).setBackground(backgroundColor) // WRAP only the Kit Rows if(i == 6 || i == 7){ targetWorksheet.getRange(rowStart + i, columnStart + j).setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP); } else{ targetWorksheet.getRange(rowStart + i, columnStart + j).setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP); } targetWorksheet.getRange(rowStart + i, columnStart + j).setValue(text).setFontWeight(fontWeight).setFontSize(fontSize).setFontColor(fontColor).setHorizontalAlignment(textHorAlignment).setVerticalAlignment(textVerAlignment); } } }

在此处输入图像描述

在此处输入图像描述

I believe your goal is as follows.我相信你的目标如下。

  • You want to copy the sheet by fixing the values.您想通过固定值来复制工作表。

In your situation, how about the following sample script?在您的情况下,以下示例脚本怎么样?

Sample script:示例脚本:

Please set the source Spreadsheet ID, source sheet name, destination Spreadsheet ID and destination sheet name.请设置源电子表格 ID、源表格名称、目标电子表格 ID 和目标表格名称。

function sample() {
  const srcSpreadsheetId = "###"; // Please set the source Spreadsheet ID.
  const srcSheetName = "Send Data"; // Please set the source sheet name.
  const dstSpreadsheetId = "###"; // Please set the destination Spreadsheet ID.
  const dstSheetName = "###"; // Please set the destination sheet name.

  // Process at source Spreadsheet.
  const srcSS = SpreadsheetApp.openById(srcSpreadsheetId);
  const srcSheet = srcSS.getSheetByName(srcSheetName);
  const temp1 = srcSheet.copyTo(srcSS);
  temp1.deleteRow(1);
  const range1 = temp1.getDataRange();
  range1.copyTo(range1, { contentsOnly: true });
  range1.clearDataValidations();

  // Process at destination Spreadsheet.
  const dstSS = SpreadsheetApp.openById(dstSpreadsheetId);
  const dstSheet = dstSS.getSheetByName(dstSheetName);
  const temp2 = temp1.copyTo(dstSS);
  const range2 = temp2.getDataRange();
  range2.copyTo(dstSheet.getRange("A1"));

  // Remove template sheets.
  srcSS.deleteSheet(temp1);
  dstSS.deleteSheet(temp2);
}
  • In your showing script, it seems that the values, the background colors, the font styles, and so on are trying to be copied.在您的显示脚本中,似乎正在尝试复制值、背景 colors、字体 styles 等。 In this case, I thought that the script might be a bit complicated.在这种情况下,我认为脚本可能有点复杂。 So, in this sample script, the sheet is copied as the fixed values.因此,在此示例脚本中,工作表被复制为固定值。 I thought that by this, the script might be a bit simple.我认为这样,脚本可能有点简单。

References:参考:

暂无
暂无

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

相关问题 使用Google脚本将数据从一个Google电子表格拉到另一电子表格 - Pull data from one Google spreadsheet to another using Google script 将数据从一个电子表格复制到另一种保留格式且不使用公式 - Copy data from one Spreadsheet to another keeping format and without formulas 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 如何从一张工作表中获取Google Spreadsheet中的数据行,并预先填充另一张工作表 - How to take rows of data in Google Spreadsheet from one sheet, and pre-populate another sheet Google Apps脚本:如何从一个电子表格中提取今天输入的数据并将其转换为另一电子表格? - Google Apps Script: How do I pull data entered today from one spreadsheet and transpose it to another? 在 Google Apps 脚本中从另一个电子表格编辑一个电子表格 - Editing one Spreadsheet from another Spreadsheet in Google Apps Script 如何在编辑时将数据从一个电子表格复制到另一个电子表格 - How to copy data from one spreadsheet to another on edit 使用 Google Apps 脚本将数据范围从一个 Google 电子表格导出到另一个 - Export Range of Data from One Google Spreadsheet to Another using Google Apps Script 在编辑时将数据从一个电子表格复制到另一个电子表格 - Copy data from one spreadsheet to another on edit 从Google Spreadsheet导出到Firebase,错误 - Exporting from Google Spreadsheet to Firebase, ERROR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM