从另一个Google工作表导入带有公式的数据并删除原始数据 [英] Importing data with formula from another google sheet and deleting original data

查看:93
本文介绍了从另一个Google工作表导入带有公式的数据并删除原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本从另一个Google表格导入数据,但没有引入我需要的公式.

I am using the following script to import data from another Google Sheet but it doesn´t bring in the formula I need with it.

我尝试了ImportRange,但是这似乎只是同步了两张纸,因此我需要删除原始纸(表1)中的数据,但是也要删除我购买的纸(主表)中的数据.

I have tried ImportRange but that seems to just sync the two sheets and I need to delete the data in the original sheet (Sheet 1) but that also deletes it in the sheet I bought in to (Master Sheet).

还有另一种方法可以做到这一点吗?

Is there another way to accomplish this?

function Copy() {
  var sss = SpreadsheetApp.openById('Sheet 1 ID');
  var ss = sss.getSheetByName('results');
  var range = ss.getRange('2:688');
  var data = range.getValues();
  var tss = SpreadsheetApp.openById('Master Sheet ID');
  var ts = tss.getSheetByName('results');
  ts.getRange(ts.getLastRow() + 1, 1, data.length, data[0].length).setValues(
    data
  );
  var sheet = SpreadsheetApp.openById('Sheet 1 ID').getSheetByName('Roger');
  ss.getRange('2:688').clearContent();
}

推荐答案

问题:

  • 不能使用getValues()
  • 检索公式

    Issue:

    • Formulas are not retrieved with getValues()
      • 使用range#getFormulasrange#getValues并混合这两个数组以创建一个公式和值数组,以供以后使用setValues().
      • 使用高级Google服务来访问Sheets API以直接获取组合.
      • Use range#getFormulas and range#getValues and mix those two arrays to create a array of formulas and values to setValues() later.
      • Use Advanced Google Services to access Sheets API to directly get the mix.
        var data = range.getValues();
        data = range.getFormulas().map(function(e, i) {//i=index of row(e)
          return e.map(function(f, j) {//j = index of column(f)
            return f === "" ? data[i][j] : f;
          });
        });
      

        var req = {
          ranges: 'results!2:688',
          valueRenderOption: 'FORMULA', //get formulas with values
        };
        var r = Sheets.Spreadsheets.Values.batchGet('Sheet 1 ID', req);
        data = r.valueRanges[0].values;
      

      参考文献:

      • getFormulas
      • 启用AdvancedServices
      • batchGet
      • References:

        • getFormulas
        • EnablingAdvancedServices
        • batchGet
        • 这篇关于从另一个Google工作表导入带有公式的数据并删除原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆